fileupload上传,支持多文件上传
来源:互联网 发布:linux 如何执行文件 编辑:程序博客网 时间:2024/05/17 04:19
我这用的是commons-fileupload-1.3.1.jar,commons-io-1.4.jar
可能有些人sfu.parseRequest(request),这个为空,这是为什么,是因为,在前端的表单中,file的name需要指定。
上传文件类型,大小,这些我都没判断了。
import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;public class Uploadutil {public static boolean upload(HttpServletRequest request,String uploadPath){final int SizeThreshold=1 * 1024 * 1024;//指定在内存中缓存数据大小final int FileSizeMax=5 * 1024 * 1024;//指定单个上传文件的最大尺寸final int SizeMax=10 * 1024 * 1024;//指定一次上传多个文件的总尺寸SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");//初始化上传文件的临时存放目录,必须是绝对路径File tempDir=new File("c://temp");try { if (ServletFileUpload.isMultipartContent(request)) { DiskFileItemFactory factory = new DiskFileItemFactory(); //指定在内存中缓存数据大小,单位为byte,这里设为1Mb factory.setSizeThreshold(SizeThreshold); //设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录 factory.setRepository(tempDir); ServletFileUpload sfu = new ServletFileUpload(factory); // 指定单个上传文件的最大尺寸,单位:字节,这里设为5Mb sfu.setFileSizeMax(FileSizeMax); //指定一次上传多个文件的总尺寸,单位:字节,这里设为10Mb sfu.setSizeMax(SizeMax); //设置编码,因为我的jsp页面的编码是utf-8的 sfu.setHeaderEncoding("UTF-8"); List <FileItem> fileItems = sfu.parseRequest(request); uploadPath = uploadPath + "upload//"; //选定上传的目录此处为当前目录,没有则创建 if (!new File(uploadPath).isDirectory()){ new File(uploadPath).mkdirs(); } int leng = fileItems.size(); for(int n=0;n<leng;n++) { FileItem item = fileItems.get(n); // 从集合中获得一个文件流 // 如果是普通表单字段 if(item.isFormField()) { String name = item.getFieldName(); // 获得该字段名称 String value = item.getString("utf-8"); //获得该字段值 System.out.println(name+value); }else if(item.getName().length()>0) { // 如果为文件域 String iname = item.getName().substring( item.getName().lastIndexOf(".")); String fname=sdf.format(new Date())+"-"+n+iname; try { item.write(new File(uploadPath, fname)); // 写入文件 } catch (Exception e) { e.printStackTrace(); } } } } }catch (Exception e) { e.printStackTrace(); } return true;}}
import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.yo.util.Uploadutil;public class uploadServlet extends HttpServlet {public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setCharacterEncoding("utf-8");request.setCharacterEncoding("utf-8");String uploadPath = getServletContext().getRealPath("/"); Uploadutil.upload(request, uploadPath);System.out.println("成功了");}}
0 0
- fileupload上传,支持多文件上传
- common-fileupload:支持中文、大文件上传的上传组件
- fileupload上传多个文件
- commons-fileupload 多文件上传
- 文件上传控件(FileUpload)
- 使用FileUpload上传文件
- common-fileupload上传文件
- java fileupload上传文件
- FileUpload的文件上传
- 文件上传 FileUpload
- FileUpload上传读取文件
- FileUpload(文件上传)
- 文件上传 commons-FileUpload
- extjs 上传文件 fileUpload
- common-fileupload上传文件
- extjs 上传文件 fileUpload
- Apache FileUpload文件上传
- 文件上传FileUpload组件
- UVA-12083-Guardian of Decency
- markdown在ubuntu上的使用
- LeetCode:Interleaving String
- Ubuntu——显示详细开机信息
- BufferedReader和BufferedWriter
- fileupload上传,支持多文件上传
- Linux主机公钥/私钥配置
- DataContext ConnectionString
- HDOJ-2586(Tarjan算法)
- Unity3d NGUI的使用(十三)(UITextList制作聊天窗口)
- Pushing the Limits of User-Defined Runtime Attributes in Interface Builder
- 2014中国软件开发者调查(三):移动应用、游戏开发技术应用特点【转】
- 关于UIImage自动释放问题
- Unity3d NGUI的使用(十四)(NGUI制作Tab形式UI)