jsp上传下载文件
上一篇 / 下一篇 2012-04-24 22:58:11 / 个人分类:jsp
上传文件方法很多,大多用到组件。这里用的是jspsmart,功能很简单,但是够用了。可参考http://wenku.baidu.com/view/5781fad7b9f3f90f76c61b86.html和http://developer.51cto.com/art/200907/134497.htm。
来自于:http://www.cnblogs.com/badwood316/archive/2012/04/24/2468450.html
上传文件代码:
1 SmartUpload su = new SmartUpload();//必须最先调用
2 su.initialize(pageContext);//必须先初始化
3 //设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
4 su.setAllowedFilesList("doc,txt,pdf");
5 //jsp,htm,html扩展名的文件和没有扩展名的文件。
6 su.setDeniedFilesList("exe,bat,jsp,htm,html");
7 // 上传文件
8 su.upload();
9 // 将上传文件全部保存到指定目录
10 int count = su.save(uploadpath);//,SmartUpload.SAVE_VIRTUAL);
11 com.jspsmart.upload.Request mrequest = su.getRequest();
12 //利用Request对象获取参数之值
13 String act = mrequest.getParameter("act")==null?"":mrequest.getParameter("act");
14 String title = mrequest.getParameter("d_title")==null?"":mrequest.getParameter("d_title");
15 String abstracts = mrequest.getParameter("d_abstracts")==null?"":mrequest.getParameter("d_abstracts");
16 String author = mrequest.getParameter("d_author")==null?"":mrequest.getParameter("d_author");
17 int year = 2012;
18 try{
19 year = Integer.parseInt(mrequest.getParameter("d_year"));
20 }catch(Exception ex){
21 year = 2012;
22 }
23 int journals = 1;
24 try{
25 journals = Integer.parseInt(mrequest.getParameter("d_journals"));
26 }catch(Exception ex){
27 journals = 1;
28 }
29 int mid = 1;
30 try{
31 mid = Integer.parseInt(mrequest.getParameter("mid"));
32 }catch(Exception ex){
33 mid = 0;
34 }
35
36 // 逐一提取上传文件信息,同时可保存文件。
37 if(su.getFiles().getCount() >0)
38 {
39 com.jspsmart.upload.File file = su.getFiles().getFile(0);
40 try{
41 Connection con=DriverManager.getConnection(dburl) ;
42 PreparedStatement stmt;
43 int rst = 0;
44 FileInputStream inf = null;
45 inf = new FileInputStream(uploadpath+"/"+file.getFileName());
46 int length = inf.available();
47 if(mid>0){//update old news
48 stmt = con.prepareStatement("update magazines set title=?,abstracts=?,author=?,year=?,journals=?,filename=?,filetype=?,topic=? where mid=?");
49 stmt.setString(1,title);
50 stmt.setString(2,abstracts);
51 stmt.setString(3,author);
52 stmt.setInt(4,year);
53 stmt.setInt(5,journals);
54 stmt.setString(6,file.getFileName());
55 stmt.setString(7,file.getFileExt());
56 stmt.setBinaryStream(8,inf,length);
57 stmt.setInt(9,mid);
58 //out.println("<script>alert('do update')</script>");
59 }else{//add new news
60 stmt = con.prepareStatement("insert into magazines(title,abstracts,author,year,journals,filename,filetype,topic) values(?,?,?,?,?,?,?,?)");
61 stmt.setString(1,title);
62 stmt.setString(2,abstracts);
63 stmt.setString(3,author);
64 stmt.setInt(4,year);
65 stmt.setInt(5,journals);
66 stmt.setString(6,file.getFileName());
67 stmt.setString(7,file.getFileExt());
68 stmt.setBinaryStream(8,inf,length);
69 //out.println("<script>alert('do insert')</script>");
70 }
71 rst=stmt.executeUpdate();
72 inf.close();
73 java.io.File myDelFile=new java.io.File(uploadpath+"/"+file.getFileName());
74 if(myDelFile.exists())
75 {
76 myDelFile.delete();
77 }
78
79 if(rst>0){
80 out.println("<script>alert(\"修改成功\");</script>");
81 }else{
82 out.println("<script>alert(\"修改失败\");</script>");
83 }
84 stmt.close();
85 con.close();
86 //response.sendRedirect("qkgl.jsp");
87 }catch(Exception ex){
88 ex.printStackTrace();
89 }
90 }
2 su.initialize(pageContext);//必须先初始化
3 //设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
4 su.setAllowedFilesList("doc,txt,pdf");
5 //jsp,htm,html扩展名的文件和没有扩展名的文件。
6 su.setDeniedFilesList("exe,bat,jsp,htm,html");
7 // 上传文件
8 su.upload();
9 // 将上传文件全部保存到指定目录
10 int count = su.save(uploadpath);//,SmartUpload.SAVE_VIRTUAL);
11 com.jspsmart.upload.Request mrequest = su.getRequest();
12 //利用Request对象获取参数之值
13 String act = mrequest.getParameter("act")==null?"":mrequest.getParameter("act");
14 String title = mrequest.getParameter("d_title")==null?"":mrequest.getParameter("d_title");
15 String abstracts = mrequest.getParameter("d_abstracts")==null?"":mrequest.getParameter("d_abstracts");
16 String author = mrequest.getParameter("d_author")==null?"":mrequest.getParameter("d_author");
17 int year = 2012;
18 try{
19 year = Integer.parseInt(mrequest.getParameter("d_year"));
20 }catch(Exception ex){
21 year = 2012;
22 }
23 int journals = 1;
24 try{
25 journals = Integer.parseInt(mrequest.getParameter("d_journals"));
26 }catch(Exception ex){
27 journals = 1;
28 }
29 int mid = 1;
30 try{
31 mid = Integer.parseInt(mrequest.getParameter("mid"));
32 }catch(Exception ex){
33 mid = 0;
34 }
35
36 // 逐一提取上传文件信息,同时可保存文件。
37 if(su.getFiles().getCount() >0)
38 {
39 com.jspsmart.upload.File file = su.getFiles().getFile(0);
40 try{
41 Connection con=DriverManager.getConnection(dburl) ;
42 PreparedStatement stmt;
43 int rst = 0;
44 FileInputStream inf = null;
45 inf = new FileInputStream(uploadpath+"/"+file.getFileName());
46 int length = inf.available();
47 if(mid>0){//update old news
48 stmt = con.prepareStatement("update magazines set title=?,abstracts=?,author=?,year=?,journals=?,filename=?,filetype=?,topic=? where mid=?");
49 stmt.setString(1,title);
50 stmt.setString(2,abstracts);
51 stmt.setString(3,author);
52 stmt.setInt(4,year);
53 stmt.setInt(5,journals);
54 stmt.setString(6,file.getFileName());
55 stmt.setString(7,file.getFileExt());
56 stmt.setBinaryStream(8,inf,length);
57 stmt.setInt(9,mid);
58 //out.println("<script>alert('do update')</script>");
59 }else{//add new news
60 stmt = con.prepareStatement("insert into magazines(title,abstracts,author,year,journals,filename,filetype,topic) values(?,?,?,?,?,?,?,?)");
61 stmt.setString(1,title);
62 stmt.setString(2,abstracts);
63 stmt.setString(3,author);
64 stmt.setInt(4,year);
65 stmt.setInt(5,journals);
66 stmt.setString(6,file.getFileName());
67 stmt.setString(7,file.getFileExt());
68 stmt.setBinaryStream(8,inf,length);
69 //out.println("<script>alert('do insert')</script>");
70 }
71 rst=stmt.executeUpdate();
72 inf.close();
73 java.io.File myDelFile=new java.io.File(uploadpath+"/"+file.getFileName());
74 if(myDelFile.exists())
75 {
76 myDelFile.delete();
77 }
78
79 if(rst>0){
80 out.println("<script>alert(\"修改成功\");</script>");
81 }else{
82 out.println("<script>alert(\"修改失败\");</script>");
83 }
84 stmt.close();
85 con.close();
86 //response.sendRedirect("qkgl.jsp");
87 }catch(Exception ex){
88 ex.printStackTrace();
89 }
90 }
下载文件代码:
1 int mid = 0;
2 String title="";
3 try{
4 mid = Integer.parseInt(request.getParameter("mid"));
5 }catch(Exception ex){
6 mid = 0;
7 }
8 if(mid==0){
9 out.println("<script>alert('无此文档');</script>");
10 }else{
11 try{
12 Connection con=DriverManager.getConnection(dburl) ;
13 PreparedStatement stmt = con.prepareStatement("select * from magazines where mid=?");
14 stmt.setInt(1,mid);
15 ResultSet rst = stmt.executeQuery();
16 if(rst.next()){
17 String fn = rst.getString("filename");
18 if(fn==null){
19 out.println("</script>alert('无此文件');</script>");
20 }else{
21 String filename = URLEncoder.encode(fn,"UTF-8");
22 //response.addHeader("Content-Disposition", "attachment;filename=" + filename);
23 java.io.File temp = java.io.File.createTempFile("temp","."+rst.getString("filetype"));
24 InputStream infile = rst.getBinaryStream("topic");
25 FileOutputStream file = null;
26 file = new FileOutputStream (temp);
27 int chunk;
28 while ((chunk = infile.read()) != -1)
29 file.write(chunk);
30 file.close();
31 infile.close();
32
33 SmartUpload su = new SmartUpload();
34 // 初始化
35 su.initialize(pageContext);
36 // 设定contentDisposition为null以禁止浏览器自动打开文件,
37 //保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
38 //doc时,浏览器将自动用word打开它。扩展名为pdf时,
39 //浏览器将用acrobat打开。
40 su.setContentDisposition(null);
41 // 下载文件
42 su.downloadFile(temp.getAbsolutePath());
43 out.clear();
44 out = pageContext.pushBody();
45 //out.println(temp.getAbsolutePath());
46 //out.println(fn);
47 temp.delete();
48 }
49 }
50 }catch(Exception ex){
51 ex.printStackTrace();
52 }
53 }
2 String title="";
3 try{
4 mid = Integer.parseInt(request.getParameter("mid"));
5 }catch(Exception ex){
6 mid = 0;
7 }
8 if(mid==0){
9 out.println("<script>alert('无此文档');</script>");
10 }else{
11 try{
12 Connection con=DriverManager.getConnection(dburl) ;
13 PreparedStatement stmt = con.prepareStatement("select * from magazines where mid=?");
14 stmt.setInt(1,mid);
15 ResultSet rst = stmt.executeQuery();
16 if(rst.next()){
17 String fn = rst.getString("filename");
18 if(fn==null){
19 out.println("</script>alert('无此文件');</script>");
20 }else{
21 String filename = URLEncoder.encode(fn,"UTF-8");
22 //response.addHeader("Content-Disposition", "attachment;filename=" + filename);
23 java.io.File temp = java.io.File.createTempFile("temp","."+rst.getString("filetype"));
24 InputStream infile = rst.getBinaryStream("topic");
25 FileOutputStream file = null;
26 file = new FileOutputStream (temp);
27 int chunk;
28 while ((chunk = infile.read()) != -1)
29 file.write(chunk);
30 file.close();
31 infile.close();
32
33 SmartUpload su = new SmartUpload();
34 // 初始化
35 su.initialize(pageContext);
36 // 设定contentDisposition为null以禁止浏览器自动打开文件,
37 //保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
38 //doc时,浏览器将自动用word打开它。扩展名为pdf时,
39 //浏览器将用acrobat打开。
40 su.setContentDisposition(null);
41 // 下载文件
42 su.downloadFile(temp.getAbsolutePath());
43 out.clear();
44 out = pageContext.pushBody();
45 //out.println(temp.getAbsolutePath());
46 //out.println(fn);
47 temp.delete();
48 }
49 }
50 }catch(Exception ex){
51 ex.printStackTrace();
52 }
53 }
来自于:http://www.cnblogs.com/badwood316/archive/2012/04/24/2468450.html
TAG:
追随自己的梦想,永不言弃。
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | ||||||
5 | 6 | 7 | 8 | 9 | 10 | 11 | |||
12 | 13 | 14 | 15 | 16 | 17 | 18 | |||
19 | 20 | 21 | 22 | 23 | 24 | 25 | |||
26 | 27 | 28 | 29 | 30 | 31 |
我的存档
数据统计
- 访问量: 71973
- 日志数: 62
- 文件数: 1
- 书签数: 4
- 建立时间: 2011-01-27
- 更新时间: 2012-06-16