java文件的上传和下载_java 文件的上传和下载

1 packagecom.dkt.servlet;2

3 importjava.io.File;4 importjava.io.IOException;5 importjava.sql.SQLException;6 importjava.util.ArrayList;7

8 importjavax.servlet.ServletException;9 importjavax.servlet.http.HttpServlet;10 importjavax.servlet.http.HttpServletRequest;11 importjavax.servlet.http.HttpServletResponse;12

13 importorg.lxh.smart.Files;14 importorg.lxh.smart.SmartUpload;15 importorg.lxh.smart.SmartUploadException;16

17 public class FileUpdownLoad extendsHttpServlet {18

19 public voiddoGet(HttpServletRequest request, HttpServletResponse response)20 throwsServletException, IOException {21 doPost(request, response);22 }23

24 public voiddoPost(HttpServletRequest request, HttpServletResponse response)25 throwsServletException, IOException {26

27 response.setContentType("text/html");28 request.setCharacterEncoding("GB2312");29 response.setCharacterEncoding("GB2312");30 SmartUpload su = newSmartUpload();31

32 String op = request.getParameter("op");33 if (("upload").equals(op)) {34 su.initialize(getServletConfig(), request, response);//初始化

35 su.setAllowedFilesList("txt,jpg,png,docx,xml,PNG,xlsx");//允许上传的文件类型,区分大小写

36 try{37 su.setDeniedFilesList("html,exe");//不允许上传的文件类型

38 } catch(SQLException e) {39 e.printStackTrace();40 }41 su.setMaxFileSize(5000000);//文件大小

42

43 /*Files files = su.getFiles();44 org.lxh.smart.File file2 = files.getFile(0);45 System.out.println(file2.getFileName());46 for(int i=0;i"+fileName);50 fileName = new String(fileName.getBytes("ISO-8859-1"),"GB2312");51 try {52 file.saveAs(fileName);53 } catch (SmartUploadException e) {54 e.printStackTrace();55 }56 }*/

57 try{58 su.upload();//上传

59 su.save("/smartUpdownload");//上传的路径,在tomcat下的文件夹中

60 System.out.println("上传成功!!!");61 } catch(SmartUploadException e) {62 e.printStackTrace();63 }64 request.getRequestDispatcher("fileUpDownload.jsp").forward(request, response);65 }else if(("showFiles").equals(op)){66 //得到上传到服务器的文件夹

67 String realPath = this.getServletContext().getRealPath("smartUpdownload");68 //得到所有文件的文件名,并显示在页面上

69 File file = newFile(realPath);70 File[] listFiles =file.listFiles();71 ArrayList list = new ArrayList();72 for(File fi : listFiles) {73 list.add(fi.getName());//把所有的文件名存入集合

74 }75 request.setAttribute("list", list);76 request.getRequestDispatcher("fileUpDownload.jsp").forward(request, response);77 }else if(("download").equals(op)){78 su.initialize(getServletConfig(), request, response);//初始化

79 su.setContentDisposition(null);80 String filename1 = request.getParameter("filename");81 String filename = new String(filename1.getBytes("ISO-8859-1"), "GB2312");82 System.out.println(filename);83 try{84 su.downloadFile("/smartUpdownload/"+filename);85 } catch(SmartUploadException e) {86 e.printStackTrace();87 }88 }89

90

91

92

93 }94

95 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值