zoukankan      html  css  js  c++  java
  • java commons-fileupload servlet 多文件上传

    commons-fileupload servlet 多文件上传

    需要引入的 jar 包。

    commons-fileupload-1.3.2.jar

    commons-io-2.2.jar


    工程路劲:查看源码

    JSP 页面:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    		<title>批量上传</title>
    	</head>
    	<body>
    		<form name="uploadForm" method="post" action="UoloadServlet" enctype="multipart/form-data">
    			<table border="1">
    				<thead>
    					<tr>
    						<th>名称</th>
    						<th>文件</th>
    					</tr>
    				</thead>
    				<tbody>
    					<c:forEach begin="1" end="10" var="i">
    						<tr>
    							<td>文件${i}</td>
    							<td><input type="file" name="file"></td>
    						</tr>
    					</c:forEach>
    				</tbody>
    			</table>
    		<input type="submit" name="submit" value="开始上传">
    		</form>
    	</body>
    </html>

    servlet  页面

    package com.servlet;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.util.FileUtil;
    
    /**
     * Servlet implementation class uoloadServlet
     */
    public class UoloadServlet extends HttpServlet {
    
    	private static final long serialVersionUID = 1L;
    
    	/**
    	 * @see HttpServlet#HttpServlet()
    	 */
    	public UoloadServlet() {
    		
    		super();
    	}
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
    	 *      response)
    	 */
    	protected void doGet(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    		
    		doPost(request, response);
    	}
    
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
    	 *      response)
    	 */
    	protected void doPost(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    
    		// 文件上传的路径,这里设置的是服务器项目根路径下的 IMG 目录
    		String filePath = request.getSession().getServletContext().getRealPath("/")+ "IMG/";
    
    		// 获取照片上传工具类,并且批量上传照片文件
    		List<File> files = FileUtil.getInstance(filePath).upload(request);
    
    		// 将上传成功照片的照片传值到页面
    		request.setAttribute("files", files);
    
    		request.getRequestDispatcher("list.jsp").forward(request, response);
    	}
    }
    

    FileUtil.java

    package com.util;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.URLEncoder;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.FileItemFactory;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    
    public class FileUtil {
    
    	// 文件上传路径
    	private static String UPLOAD_PATH = null;
    
    	/**
    	 * @author Duke 静态内部类,实例化工具类
    	 */
    	private static class LazyHolder {
    
    		private static final FileUtil FILEUTIL = new FileUtil();
    	}
    
    	/**
    	 * 私有构造函数
    	 */
    	private FileUtil() {
    
    	}
    
    	/**
    	 * 获取工具类实例
    	 * 
    	 * @return FileUploadUtil 实例
    	 */
    	public static FileUtil getInstance(String uploadPath) {
    
    		UPLOAD_PATH = uploadPath;
    		return LazyHolder.FILEUTIL;
    	}
    
    	/**
    	 * 文件批量上传
    	 * 
    	 * @param request
    	 * @param uploadPath
    	 * 
    	 */
    	public List<File> upload(HttpServletRequest request) {
    
    		// 用于存放上传成功的照片
    		List<File> files = new ArrayList<File>();
    
    		try {
    
    			FileItemFactory factory = new DiskFileItemFactory();
    			ServletFileUpload upload = new ServletFileUpload(factory);
    
    			List<FileItem> items = upload.parseRequest(request);
    
    			for (FileItem fileItem : items) {
    				
    				// 获得文件名,包括文件路径
    				String fileFullName = fileItem.getName();
    
    				if (fileFullName != null && fileFullName != "" && fileItem.getSize() > 0) {
    
    					// 获取文件路径
    					String fileName = new File(fileFullName).getName();
    
    					// 创建文件
    					File savedFile = new File(UPLOAD_PATH, fileName);
    
    					// 如果文件所在文件夹不存在的话,新建文件夹
    					if (!savedFile.getParentFile().exists()) {
    						savedFile.getParentFile().mkdirs();
    					}
    
    					// 写入文件
    					fileItem.write(savedFile);
    					files.add(savedFile);
    				}
    			}
    		} catch (Exception e) {
    			
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    		return files;
    	}
    	
    	/**
    	 * 文件下载
    	 * 
    	 * @param filePath
    	 * @throws IOException 
    	 * @throws Exception
    	 */
    	public void downLoadFile(String filePath, HttpServletResponse response) throws IOException {
    		
    		// 处理中文乱码问题
    		filePath = new String(filePath.getBytes("ISO-8859-1"), "UTF-8");
    		
    		downLoadFile(new File(filePath), response);
    	}
    	
    	/**
    	 * 文件下载 
    	 * 
    	 * @param file
    	 * @throws IOException 
    	 */
    	public void downLoadFile(File file, HttpServletResponse response) throws IOException {
    		
    		if (file.exists()) {
    			
    			response.reset();
    			response.setContentType("application/x-download");
    			response.addHeader("Content-Disposition", "attachment; filename="" + URLEncoder.encode(file.getName(), "UTF-8"));
    			
    			// 输出文件字节流
    			writeBufferByte(file, response);
    		}
    	}
    	
    	/**
    	 * 输出照片字节流
    	 * 
    	 * @throws IOException 
    	 */
    	public void outputImgByte(String filePath, HttpServletResponse response) throws IOException {
    		
    		// 处理中文乱码问题
    		filePath = new String(filePath.getBytes("ISO-8859-1"), "UTF-8");
    		outputImgByte(new File(filePath), response);
    	}
    	
    	/**
    	 * 输出照片字节流
    	 * 
    	 * @throws IOException 
    	 */
    	public void outputImgByte(File file, HttpServletResponse response) throws IOException {
    		
    		// 文件存在的情况下输出文件
    		if (file.exists()) {
    
    			// 设置相应信息的类型
    			response.setContentType("image/jpeg");
    
    			// 输出文件字节流
    			writeBufferByte(file, response);
    		}
    	}
    	
    	/**
    	 * 输出文件字节流
    	 * 
    	 * @param file
    	 * @param response
    	 * @throws IOException
    	 */
    	private void writeBufferByte(File file, HttpServletResponse response) throws IOException {
    		
    		// 一次读 2048 个字节
    		byte[] buffer = new byte[2048];
    
    		// 获取照片流
    		FileInputStream fos = new FileInputStream(file);
    
    		int count;
    
    		while ((count = fos.read(buffer)) > 0) {
    
    			// 输出照片流
    			response.getOutputStream().write(buffer, 0, count);
    		}
    
    		fos.close();
    	}
    }

    查看源码

  • 相关阅读:
    LeetCode 326. Power of Three
    LeetCode 324. Wiggle Sort II
    LeetCode 322. Coin Change
    LeetCode 321. Create Maximum Number
    LeetCode 319. Bulb Switcher
    LeetCode 318. Maximum Product of Word Lengths
    LeetCode 310. Minimum Height Trees (DFS)
    个人站点大开发!--起始篇
    LeetCode 313. Super Ugly Number
    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (DP)
  • 原文地址:https://www.cnblogs.com/duke-cui/p/11099121.html
Copyright © 2011-2022 走看看