commons-fileupload+servlet实现简单文件上传
来源:互联网 发布:中国核弹数量知乎 编辑:程序博客网 时间:2024/06/06 02:48
这两天复习servlet,做文件上传,刚开始是这样写的
public class FileUploadServlet extends HttpServlet{ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); InputStream is = req.getInputStream(); String path = getServletContext().getRealPath("/uploads/a.jpg"); System.out.println(path); File file = new File(path); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); byte[] b = new byte[1024]; int n=0; while((n=is.read(b))!=-1){ fos.write(b,0,n); } fos.close(); is.close(); }}
发现上传图片等非文本文件是不能打开的,并且得到的文件比源文件要大,然后就想到用commons-fileupload来做上传,先上代码。
public class RegisterServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //req.setCharacterEncoding("utf-8");已无用 DiskFileItemFactory factory = new DiskFileItemFactory();<span style="white-space:pre">//设置阀值 factory.setSizeThreshold(1024);</span> ServletContext context = getServletContext(); //获取上传路径 String dir = context.getInitParameter("uploadDir"); ServletFileUpload upload = new ServletFileUpload(factory); try { List<FileItem> items = upload.parseRequest(req); Iterator<FileItem> iter = items.iterator(); while (iter.hasNext()) { FileItem item = iter.next(); //如果是普通的项 if (item.isFormField()) { System.out.println(item.getFieldName()); //获取utf-8格式的值 System.out.println(item.getString("utf-8")); } //如果是文件项 else { System.out.println(item.getFieldName()); InputStream is = item.getInputStream(); String path = getServletContext().getRealPath(dir); //获取源文件的名字 File file = new File(path,item.getName()); if(!file.exists()){ file.createNewFile(); } OutputStream os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[1024]; while((bytesRead = is.read(buffer, 0, 1024)) != -1){ os.write(buffer, 0, bytesRead); } os.close(); is.close(); } } } catch (FileUploadException e) { e.printStackTrace(); } } /** * @param item */ private void processUploadedFile(FileItem item) { } /** * @param item */ private void processFormField(FileItem item) { }
简单看一下源码,commons-fileupload组件是对rfc1867协议(有兴趣的可以看一下)的java实现与扩展,。
当上传文件时会有一个阀值(threshold),低于阀值时文件保存在内存中,高于阀值会写在临时文件里,临时文件的路径由组件指定,,对于大文件上传这样可以节省内存,保存好之后,才是我们得到输入流来写入指定路径文件。
0 0
- commons-fileupload+servlet实现简单文件上传
- Servlet利用Commons FileUpload实现文件上传
- servlet+uploadify+commons-fileupload实现文件上传
- Servlet使用commons-fileupload实现文件上传
- Servlet上传文件,commons-fileupload
- Commons Fileupload+Servlet+JQuery实现文件上传进度条
- JSP+Servlet +commons-fileupload-1.2.1实现文件上传
- 利用commons-fileupload在servlet中实现文件上传
- commons-fileupload 实现文件上传
- Commons-Fileupload 实现文件上传
- Commons-FileUpload实现文件上传
- commons-fileupload实现文件上传
- java commons-fileupload servlet 多文件上传
- 简单web文件上传,commons-fileupload
- 使用commons-fileupload上传简单实现
- commons-fileupload.jar实现文件上传
- 用commons-fileupload-1.2 实现文件上传
- 使用commons-fileupload实现文件上传
- 【九度OJ】1047【素数判断】
- 【缩点】HDU 2767 Proving Equivalences
- 在VC中使用CCheckListBox
- C++的继承操作---基类指针访问派生类问题---基类成员恢复访问属性问题
- C/C++ 回调函数详解
- commons-fileupload+servlet实现简单文件上传
- LeetCode Climbing Stairs
- Windows 8.1 读取OAKEY代码
- java-反射机制
- php 正则表达式
- 畅通工程续(最短路_弗洛伊德算法,最短路_迪杰斯特克拉算法,最短路_SPFA+前向星)
- ubuntu 14.04安装libpcap
- ios--多线程复习笔记
- 谋哥:这个时代没有比程序员更适合创业