因工作原因,已经好久没有上线也有更新博客了,比较了一下比较常用的JavaWeb上传组件。这里介绍的是Cos。网上也有很多它的介绍,总之只有经过自己用过才能知道它好不好。
一、JSP页面如下:
<body> <form method="POST" action="CosUploadServlet" ENCTYPE="multipart/form-data"> <table> <tr><td><input type="text" name="subject" /></td></tr> <tr><td><input type="file" name="file1"/></td></tr> <tr><td><input type="file" name="file2"/></td></tr> <tr><td><input type="file" name="file3"/></td></tr> <tr><td><input type="submit" value="上传"/></td></tr> </table> </form> <br/> <a href="CosDownloadServlet">下载</a> </body>
二、上传的Servlet如下:
/** * 使用cos组件实现文件上传 * * @author [email protected] * */ public class CosUploadServlet extends HttpServlet { private static final long serialVersionUID = -9116026245071140150L; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { File fileDir = new File(this.getServletContext() .getRealPath("/FileDir")); if (!fileDir.exists()) { fileDir.mkdirs(); } // 设置上传文件的大小,超过这个大小 将抛出IOException异常,默认大小是1M。 int inmaxPostSize = 10 * 1024 * 1024; MultipartRequest multirequest = null; // 上传文件重命名策略 RenamePolicyCos myRenamePolicyCos = new RenamePolicyCos(); try { // MultipartRequest()有8种构造函数! multirequest = new MultipartRequest(request, fileDir .getAbsolutePath(), inmaxPostSize, "GBK", myRenamePolicyCos); // GBK中文编码模式上传文件 String subject = multirequest.getParameter("subject");// 获取普通信息 System.out.println(subject); Enumeration<String> filedFileNames = multirequest.getFileNames(); String filedName = null; if (null != filedFileNames) { while (filedFileNames.hasMoreElements()) { filedName = filedFileNames.nextElement();// 文件文本框的名称 // 获取该文件框中上传的文件,即对应到上传到服务器中的文件 File uploadFile = multirequest.getFile(filedName); if (null != uploadFile && uploadFile.length() > 0) { System.out.println(uploadFile.getName()); System.out.println(uploadFile.getPath()); System.out.println(uploadFile.length()); } // 获取未重命名的文件名称 String Originalname = multirequest .getOriginalFileName(filedName); System.out.println(Originalname); } } } catch (Exception e) { e.printStackTrace(); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
/** * 文件命名策略 * * @author [email protected] * @date 2010-10-29 */ public class RenamePolicyCos implements FileRenamePolicy { public File rename(File uploadFile) { String newName = getNewFileName(uploadFile.getName()); File renameFile = new File(uploadFile.getParent(), newName); return renameFile; } private String getNewFileName(String fileName) { StringBuffer newName = new StringBuffer(); if (null != fileName && !"".equals(fileName)) { String type = ""; String name = ""; if (fileName.indexOf(".") != -1) { type = fileName.substring(fileName.indexOf(".")); name = fileName.substring(0, fileName.indexOf(".")); }else{ name = fileName; } newName.append(name); newName.append(getSuffix()); newName.append(type); } return newName.toString(); } private String getSuffix(){ StringBuffer suffix = new StringBuffer("_"); String now = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss").format(new Date()); suffix.append(now); suffix.append("_"); Random random = new Random(); String randomValue = String.valueOf(random.nextInt(1000)); suffix.append(randomValue); return suffix.toString(); } // public static void main(String[] args) { // RenamePolicyCos my = new RenamePolicyCos(); // System.out.println(my.getNewFileName("log.txt")); // } }
三、下载的Servlet如下:
/** * 使用cos组件实现文件下载 * * @author [email protected] * @date 2010-10-29 */ public class CosDownloadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filePath = "D:\\Tomcat\\apache-tomcat-6.0.26\\webapps\\CosUpload\\FileDir\\"; String fileName = "2010年10月安排_2010_10_29_04_52_06_178.txt"; String Originalname = "2010年10月安排.txt"; String isofilename = new String(Originalname.getBytes("gb2312"), "ISO8859-1"); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + isofilename); ServletOutputStream out = null; try { out = response.getOutputStream(); ServletUtils.returnFile(filePath + fileName, out); } catch (Exception e) { e.printStackTrace(); } finally { if (null != out) { out.close(); } } } }
关于Servlet配置如下:
<servlet> <servlet-name>CosUploadServlet</servlet-name> <servlet-class>com.mengya.servlet.CosUploadServlet</servlet-class> </servlet> <servlet> <servlet-name>CosDownloadServlet</servlet-name> <servlet-class>com.mengya.servlet.CosDownloadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CosUploadServlet</servlet-name> <url-pattern>/CosUploadServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>CosDownloadServlet</servlet-name> <url-pattern>/CosDownloadServlet</url-pattern> </servlet-mapping>
关于上传和下载需要完善的地方还有很多, 这里只是介绍了如何使用它的一些功能。
源码和jar包可以在下面下载