servlet之单个文件上传

1.添加commons-fileupload-1.2.1.jar,commons-io-2.0.jar
    关注http://commons.apache.org/proper/commons-fileupload/using.html
    
2.upload.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
	<form action="uploadServlet" method="post" enctype="multipart/form-data">
		File:<input type="file" name="file"/><br>
		DESC:<input type="text" name="desc"><br>
		<input type="submit" value="Submit">
	</form>
</body>
</html>

3.uploadServlet
package com.servlet;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * Servlet implementation class UploadServlet
 */
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		// Create a factory for disk-based file items
		DiskFileItemFactory factory = new DiskFileItemFactory();

		// Set factory constraints
		factory.setSizeThreshold(1024 * 500);
		File tempDirectory  = new File("d:\\tempDirectory");
		factory.setRepository(tempDirectory);

		// Create a new file upload handler
		ServletFileUpload upload = new ServletFileUpload(factory);

		// Set overall request size constraint
		upload.setSizeMax(1024 * 1024 * 5);

		// Parse the request
		try {
			List<FileItem> items = upload.parseRequest(request);
			for(FileItem item:items){
				if(item.isFormField()){
					
					String name = item.getFieldName();
					String value = item.getString();
					System.out.println(name+":"+value);
				}else{
					
					String fieldName = item.getFieldName();
				    String fileName = item.getName();
				    String contentType = item.getContentType();
				    boolean isInMemory = item.isInMemory();
				    long sizeInBytes = item.getSize();
				    
				    System.out.println("1-->"+fieldName);
				    System.out.println("2-->"+fileName);
				    System.out.println("3-->"+contentType);
				    System.out.println("4-->"+isInMemory);
				    System.out.println("5-->"+sizeInBytes);
				    
				    InputStream in = item.getInputStream();
				    byte[] buffer = new byte[1024];
				    int len =0;
				    
				    fileName="D:\\files\\"+fileName;
				    System.out.println("6-->"+fileName);
				    OutputStream out = new FileOutputStream(fileName);
				    while((len = in.read(buffer))!=-1){
				    	out.write(buffer, 0, len);
				    }
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		
		
		
	}

}


你可能感兴趣的:(servlet之单个文件上传)