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 }