?? 用java實現附件的上傳和下載.txt
字號:
用java實現附件的上傳和下載(struts)
發表時間:2007年7月4日 17時47分53秒 本文鏈接:http://user.qzone.qq.com/326855173/blog/2評論/閱讀(1/23)
用java實現附件的上傳和下載(struts)
我用的是struts組件來實現附件的上傳,把附件保存到oracle數據庫中
建表的SQL語句是
-- Create table
create table TMEP_ABC
(
ID VARCHAR2(30) primary key,
LR1 BLOB,
FILENAME1 VARCHAR2(300)
)
把附件保存的表中的方法
public void save(FormFile file1)
{
Connection cn=null;
Statement stat=null;
ResultSet rs=null;
String name1=file1.getFileName();
long id=getMaxId();
try{
cn=getConnection();
cn.setAutoCommit(false);
stat=cn.createStatement();
String sql="insert into tmep_abc(id,filename1,lr1) values('"+id+"','"+name1+"',EMPTY_BLOB())";
stat.executeUpdate(sql);
rs = stat.executeQuery("SELECT lr1 FROM tmep_abc WHERE ID='"+id+"' FOR UPDATE");
if (rs.next()) {
/* 取出此BLOB對象 */
oracle.sql.BLOB blob1 = (oracle.sql.BLOB)rs.getBlob("lr1");
/* 向BLOB對象中寫入數據 */
BufferedOutputStream out1 = new BufferedOutputStream(blob1.getBinaryOutputStream());
out1.write(file1.getFileData());
out1.close();
}
/* 正式提交 */
cn.commit();
//cn.setAutoCommit(true);
}
catch(Exception ex){
ex.printStackTrace();
try {
cn.rollback();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
finally
{
if(rs!=null)
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(stat!=null)
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(cn!=null)
try {
cn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
下載附件的方法為:
id為表的主鍵
public void getDownFile(String id,HttpServletResponse response)
{
String sql="SELECT lr1,filename1 FROM tmep_abc where id='"+id+"'";
InputStream in=null;
Connection cn=null;
Statement stat=null;
ResultSet rs=null;
BufferedOutputStream output = null;
BufferedInputStream input = null;
String temp="";
try{
cn=getConnection();
stat=cn.createStatement();
rs=stat.executeQuery(sql);
if(rs.next())
{
byte[] buffer = new byte[1024];
temp=rs.getString("filename1");
java.sql.Blob blob = rs.getBlob("lr1");
in= blob.getBinaryStream();
response.reset();
response.setContentType("application/octet-stream; charset=gb2312");
try {
temp=URLEncoder.encode(temp, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename="
+ temp);
System.out.println();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(in);
int n = (-1);
do {
n = input.read(buffer, 0, buffer.length);
if (n != (-1))
output.write(buffer, 0, n);
} while (n != (-1));
response.flushBuffer();
}
}
catch(Exception ex){}
finally
{
if (input != null)
try {
input.close();
} catch (IOException e) {
}
if (output != null)
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
if(rs!=null)
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(stat!=null)
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(cn!=null)
try {
cn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -