fileupload上传,支持多文件上传

徐涵亮
2023-12-01

我这用的是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("成功了");
	}
}


 类似资料: