当前位置: 技术问答>java相关
100分求原码(上传下载.doc)
来源: 互联网 发布时间:2015-09-07
本文导语: 100分求上传下载原码,一定给分: 哪位高手有把Word文档上传到服务器或在客户端下载服务器的Word文件的原码,最好是Jsp页面通过Servlet处理。效果好在加100分,君子一言,驷马难追!也可以发Email:ox9697@sina.com ...
100分求上传下载原码,一定给分:
哪位高手有把Word文档上传到服务器或在客户端下载服务器的Word文件的原码,最好是Jsp页面通过Servlet处理。效果好在加100分,君子一言,驷马难追!也可以发Email:ox9697@sina.com
哪位高手有把Word文档上传到服务器或在客户端下载服务器的Word文件的原码,最好是Jsp页面通过Servlet处理。效果好在加100分,君子一言,驷马难追!也可以发Email:ox9697@sina.com
|
//上传用户的附件
String filename = (String)request.getParameter("file.filename") ;
String inputfile = (String)request.getParameter("file") ;
out.println(filename);
if ( filename != null && filename.length() != 0)
{//没有文件上传
if (!filename.toUpperCase().endsWith(".ZIP") &&!filename.toUpperCase().endsWith(".RAR") && !filename.toUpperCase().endsWith(".TXT") && !filename.toUpperCase().endsWith(".DOC") )
{
out.println("alert('上传的文件必须是zip或者rar格式的压缩文件或者以.TXT,.DOC结尾的文本文件');");
return;
}
String savedDir = application.getRealPath("/netoffice/OA/personal-mgmt/Zhaopin/seekdir/")+jobapp_no+"/" ;
File ttfile = new File(savedDir);
if ( !ttfile.exists() ) ttfile.mkdirs();
FileInputStream is = new FileInputStream(inputfile); //定义文件输入流
String outputfile = savedDir +"seeker"+ filename.substring(filename.length()-4).toLowerCase();
FileOutputStream os = new FileOutputStream(outputfile);
int ch;
while ((ch = is.read()) >= 0) os.write(ch);
is.close();
os.close();
}
String filename = (String)request.getParameter("file.filename") ;
String inputfile = (String)request.getParameter("file") ;
out.println(filename);
if ( filename != null && filename.length() != 0)
{//没有文件上传
if (!filename.toUpperCase().endsWith(".ZIP") &&!filename.toUpperCase().endsWith(".RAR") && !filename.toUpperCase().endsWith(".TXT") && !filename.toUpperCase().endsWith(".DOC") )
{
out.println("alert('上传的文件必须是zip或者rar格式的压缩文件或者以.TXT,.DOC结尾的文本文件');");
return;
}
String savedDir = application.getRealPath("/netoffice/OA/personal-mgmt/Zhaopin/seekdir/")+jobapp_no+"/" ;
File ttfile = new File(savedDir);
if ( !ttfile.exists() ) ttfile.mkdirs();
FileInputStream is = new FileInputStream(inputfile); //定义文件输入流
String outputfile = savedDir +"seeker"+ filename.substring(filename.length()-4).toLowerCase();
FileOutputStream os = new FileOutputStream(outputfile);
int ch;
while ((ch = is.read()) >= 0) os.write(ch);
is.close();
os.close();
}
|
package mshtang.large;
import java.sql.*;
import java.io.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;
import mshtang.html.DatabaseForHtml;
public class LargeObjectAction
{
public void orablob()
{
}
public static void insertblob()
{
try
{
//首先是将文件输入到数据库。
Class.forName("oracle.jdbc.driver.OracleDriver");//注册数据库引擎。
String dbURL = "jdbc:oracle:thin:@192.168.0.74:1521:orcl";
String user = "pm";
String passWord = "pm";
Connection conn = DriverManager.getConnection(dbURL, user, passWord);
conn.setAutoCommit(false);//关闭自动提交,以提高性能。
Statement stmt = conn.createStatement();//建立会话
DatabaseForHtml sqlBean = new DatabaseForHtml();
String sqlStr = "";
String fileName = "";
String displayMessage = "";
try
{
sqlStr = "select table_name from user_tables where table_name='JPGTEST'";
if(!sqlBean.hasResult(conn, sqlStr))
{
displayMessage = (stmt.executeUpdate("create table JPGTEST (fname varchar2(600), bx blob)") == 0) ? "table created successfuly" : "table created failure";
System.out.println(displayMessage);
}
File file = new File("C:\25.jpg");
fileName = file.getName();
FileInputStream inStream = new FileInputStream(file);//创建输入流,将外部文件输入到InputStream 中。
byte[] buffer = new byte[inStream.available()];
sqlStr = "INSERT INTO JPGTEST VALUES ('" + fileName + "', empty_blob())";
stmt.execute(sqlStr);
ResultSet rs = stmt.executeQuery("select bx from JPGTEST where fname='" + fileName + "' for update");
if(rs.next())
{
BLOB blob = ((OracleResultSet)rs).getBLOB("bx");
OutputStream outStream = blob.getBinaryOutputStream();
inStream.read(buffer);
outStream.write(buffer);
outStream.flush();
stmt.execute("commit");
outStream.close();
System.out.println("文件成功写入数据库");
}
inStream.close();
rs.close();
}
catch(SQLException e)
{
e.printStackTrace();
System.out.println("数据库异常:" + e.getMessage());
}
//以下是从库中读取文件。
sqlStr = "SELECT * from JPGTEST where fname='" + fileName + "'";
System.out.println(sqlStr);
ResultSet rset = stmt.executeQuery(sqlStr);
if(rset.next())
{
BLOB blob = ((OracleResultSet)rset).getBLOB("bx");//获取文件字段。
InputStream inStream = blob.getBinaryStream();//建立输入流,并将字段bx的值以流的形式,放入inStream变量。
File binaryFile = new File("D:\test\" + fileName);
FileOutputStream fileOutStream = new FileOutputStream(binaryFile);//创建文件输出流。
int by = inStream.read();
while(by != -1)
{
fileOutStream.write(by);
by = inStream.read();
}
fileOutStream.flush();
System.out.println("文件从数据库中成功读出");
inStream.close();//关闭流
fileOutStream.close();
}
rset.close();
stmt.close();
conn.close();
}
catch(Exception ee)
{
System.out.println(ee.getMessage());
}
}
public static void insertLongRow()
{
try
{
//首先是将文件输入到数据库。
Class.forName("oracle.jdbc.driver.OracleDriver");//注册数据库引擎。
String dbURL = "jdbc:oracle:thin:@192.168.0.74:1521:orcl";
String user = "pm";
String passWord = "pm";
Connection conn = DriverManager.getConnection(dbURL, user, passWord);
conn.setAutoCommit(false);//关闭自动提交,以提高性能。
Statement stmt = conn.createStatement();//建立会话
DatabaseForHtml sqlBean = new DatabaseForHtml();
String sqlStr = "";
String fileName = "";
String displayMessage = "";
try
{
sqlStr = "select table_name from user_tables where table_name='rowTestTable'";
if(!sqlBean.hasResult(conn, sqlStr))
{
// displayMessage = (stmt.executeUpdate("create table rowTestTable(fileName varchar2(50), detail long raw)") == 0) ? "table created successfuly" : "table created failure";
// System.out.println(displayMessage);
}
File file = new File("C:\工程信息管理系统-内参.doc");
fileName = file.getName();
FileInputStream inStream = new FileInputStream(file);//创建输入流,将外部文件输入到InputStream 中。
// byte[] buffer = new byte[inStream.available()];
PreparedStatement pstmt = conn.prepareStatement("insert into rowTestTable values('" + fileName + "', ?)");
pstmt.setBinaryStream (1, inStream, (int)file.length());
if(pstmt.executeUpdate() == 1)
{
System.out.println("ok");
}
else
{
System.out.println("bad");
}
inStream.close();
}
catch(SQLException e)
{
e.printStackTrace();
System.out.println("数据库异常:" + e.getMessage());
}
//以下是从库中读取文件。
sqlStr = "SELECT * from rowTestTable where fileName='" + fileName + "'";
System.out.println(sqlStr);
ResultSet rset = stmt.executeQuery(sqlStr);
if(rset.next())
{
// BLOB blob = ((OracleResultSet)rset).getBLOB("bx");//获取文件字段。
InputStream inStream = rset.getBinaryStream("detail");//建立输入流,并将字段bx的值以流的形式,放入inStream变量。
File binaryFile = new File("D:\test\" + fileName);
FileOutputStream fileOutStream = new FileOutputStream(binaryFile);//创建文件输出流。
int by = inStream.read();
while(by != -1)
{
fileOutStream.write(by);
by = inStream.read();
}
fileOutStream.flush();
System.out.println("文件从数据库中成功读出");
inStream.close();//关闭流
fileOutStream.close();
}
rset.close();
stmt.close();
conn.close();
}
catch(Exception ee)
{
System.out.println(ee.getMessage());
}
}
public static void main(String args[])
{
// insertblob();
insertLongRow();
}
}
import java.sql.*;
import java.io.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;
import mshtang.html.DatabaseForHtml;
public class LargeObjectAction
{
public void orablob()
{
}
public static void insertblob()
{
try
{
//首先是将文件输入到数据库。
Class.forName("oracle.jdbc.driver.OracleDriver");//注册数据库引擎。
String dbURL = "jdbc:oracle:thin:@192.168.0.74:1521:orcl";
String user = "pm";
String passWord = "pm";
Connection conn = DriverManager.getConnection(dbURL, user, passWord);
conn.setAutoCommit(false);//关闭自动提交,以提高性能。
Statement stmt = conn.createStatement();//建立会话
DatabaseForHtml sqlBean = new DatabaseForHtml();
String sqlStr = "";
String fileName = "";
String displayMessage = "";
try
{
sqlStr = "select table_name from user_tables where table_name='JPGTEST'";
if(!sqlBean.hasResult(conn, sqlStr))
{
displayMessage = (stmt.executeUpdate("create table JPGTEST (fname varchar2(600), bx blob)") == 0) ? "table created successfuly" : "table created failure";
System.out.println(displayMessage);
}
File file = new File("C:\25.jpg");
fileName = file.getName();
FileInputStream inStream = new FileInputStream(file);//创建输入流,将外部文件输入到InputStream 中。
byte[] buffer = new byte[inStream.available()];
sqlStr = "INSERT INTO JPGTEST VALUES ('" + fileName + "', empty_blob())";
stmt.execute(sqlStr);
ResultSet rs = stmt.executeQuery("select bx from JPGTEST where fname='" + fileName + "' for update");
if(rs.next())
{
BLOB blob = ((OracleResultSet)rs).getBLOB("bx");
OutputStream outStream = blob.getBinaryOutputStream();
inStream.read(buffer);
outStream.write(buffer);
outStream.flush();
stmt.execute("commit");
outStream.close();
System.out.println("文件成功写入数据库");
}
inStream.close();
rs.close();
}
catch(SQLException e)
{
e.printStackTrace();
System.out.println("数据库异常:" + e.getMessage());
}
//以下是从库中读取文件。
sqlStr = "SELECT * from JPGTEST where fname='" + fileName + "'";
System.out.println(sqlStr);
ResultSet rset = stmt.executeQuery(sqlStr);
if(rset.next())
{
BLOB blob = ((OracleResultSet)rset).getBLOB("bx");//获取文件字段。
InputStream inStream = blob.getBinaryStream();//建立输入流,并将字段bx的值以流的形式,放入inStream变量。
File binaryFile = new File("D:\test\" + fileName);
FileOutputStream fileOutStream = new FileOutputStream(binaryFile);//创建文件输出流。
int by = inStream.read();
while(by != -1)
{
fileOutStream.write(by);
by = inStream.read();
}
fileOutStream.flush();
System.out.println("文件从数据库中成功读出");
inStream.close();//关闭流
fileOutStream.close();
}
rset.close();
stmt.close();
conn.close();
}
catch(Exception ee)
{
System.out.println(ee.getMessage());
}
}
public static void insertLongRow()
{
try
{
//首先是将文件输入到数据库。
Class.forName("oracle.jdbc.driver.OracleDriver");//注册数据库引擎。
String dbURL = "jdbc:oracle:thin:@192.168.0.74:1521:orcl";
String user = "pm";
String passWord = "pm";
Connection conn = DriverManager.getConnection(dbURL, user, passWord);
conn.setAutoCommit(false);//关闭自动提交,以提高性能。
Statement stmt = conn.createStatement();//建立会话
DatabaseForHtml sqlBean = new DatabaseForHtml();
String sqlStr = "";
String fileName = "";
String displayMessage = "";
try
{
sqlStr = "select table_name from user_tables where table_name='rowTestTable'";
if(!sqlBean.hasResult(conn, sqlStr))
{
// displayMessage = (stmt.executeUpdate("create table rowTestTable(fileName varchar2(50), detail long raw)") == 0) ? "table created successfuly" : "table created failure";
// System.out.println(displayMessage);
}
File file = new File("C:\工程信息管理系统-内参.doc");
fileName = file.getName();
FileInputStream inStream = new FileInputStream(file);//创建输入流,将外部文件输入到InputStream 中。
// byte[] buffer = new byte[inStream.available()];
PreparedStatement pstmt = conn.prepareStatement("insert into rowTestTable values('" + fileName + "', ?)");
pstmt.setBinaryStream (1, inStream, (int)file.length());
if(pstmt.executeUpdate() == 1)
{
System.out.println("ok");
}
else
{
System.out.println("bad");
}
inStream.close();
}
catch(SQLException e)
{
e.printStackTrace();
System.out.println("数据库异常:" + e.getMessage());
}
//以下是从库中读取文件。
sqlStr = "SELECT * from rowTestTable where fileName='" + fileName + "'";
System.out.println(sqlStr);
ResultSet rset = stmt.executeQuery(sqlStr);
if(rset.next())
{
// BLOB blob = ((OracleResultSet)rset).getBLOB("bx");//获取文件字段。
InputStream inStream = rset.getBinaryStream("detail");//建立输入流,并将字段bx的值以流的形式,放入inStream变量。
File binaryFile = new File("D:\test\" + fileName);
FileOutputStream fileOutStream = new FileOutputStream(binaryFile);//创建文件输出流。
int by = inStream.read();
while(by != -1)
{
fileOutStream.write(by);
by = inStream.read();
}
fileOutStream.flush();
System.out.println("文件从数据库中成功读出");
inStream.close();//关闭流
fileOutStream.close();
}
rset.close();
stmt.close();
conn.close();
}
catch(Exception ee)
{
System.out.println(ee.getMessage());
}
}
public static void main(String args[])
{
// insertblob();
insertLongRow();
}
}
|
到www.jspsmart.com下载一个jspsmartupload的bean,用参考下面:
a.htm:
文件上传页面
文件上传
2、upload.jps:
文件上传确认
以上实现上传文件到fileupload文件夹(在您的jsp文件存放夹下,默认为doc)
下载的时候直接指向:response.sendRedirect("/faceupload/"+文件名);
a.htm:
文件上传页面
文件上传
2、upload.jps:
文件上传确认
以上实现上传文件到fileupload文件夹(在您的jsp文件存放夹下,默认为doc)
下载的时候直接指向:response.sendRedirect("/faceupload/"+文件名);
|
看这里!
http://www.jspserver.com:8080/cgi-bin/jspbbs/topic.cgi?forum=9&topic=2&show=
http://www.jspserver.com:8080/cgi-bin/jspbbs/topic.cgi?forum=9&topic=2&show=
|
用jspsmart。
|
好像有个插件可以用的