HTML form表单数据与文件混合上传

1. 将页面提交文件上传到服务器目录下面(只能是POST提交方式)
2. 需要引入commons-fileupload.jar

HTML form表单例子如下


<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>

	<form action="user.do?op=updateUserImage" id="upfile"  method="post" enctype="multipart/form-data">
	     <input type="hidden" id="fuserid" name="id" value="12334" />
	     <input type="file" id="newuserimage" name="newuserimage" value="浏览图片" />
	     <input type="submit" value="tijiao"/>
	form>

body>
html>

文件上传,这是一个封装的方法可以直接在自定义Servlet的doPost或者doGet中调用。

package com.xu.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

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

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;

@SuppressWarnings(value="all")
public class UploadFile {


	private static String fileNmae="";
	
	/**
	 * 随机文件夹名
	 * @return
	 */
	private String randomFilePath(){
		SimpleDateFormat format=new SimpleDateFormat("yyyy/MM/dd/HH");
		return format.format(new Date()).replace("/", File.separator)+File.separator;
	}

	/**
	 * 随机文件名
	 * @return
	 */
	private String randomFileName(){
		fileNmae= UUID.randomUUID().toString().substring(0,10);
		return fileNmae;
	}
	
	private String getFileName(String name){
		return name.substring(name.lastIndexOf("."));
	}
	
	/**
	 * 单文件上传
	 * @param request HttpServletRequest
	 * @param directories 在 Tomcat webapps 下面的存放文件的文件夹的文件夹名
	 * @return FileBean (JavaBean对象)
	 * @throws FileUploadException
	 * @throws IOException
	 * @throws ServletException
	 */
	public FileBean singleFileUpload(HttpServletRequest request,String directories) throws FileUploadException, IOException, ServletException{
		request.setCharacterEncoding("UTf-8");
		String realPath=new File(request.getRealPath("/")).getParent()+File.separator+directories+File.separator+randomFilePath();
		randomFileName();//获取随机文件名
		String webPath=directories+File.separator+randomFilePath()+fileNmae;
		DiskFileItemFactory factory=new DiskFileItemFactory(1024*30,new File(realPath));//创建文件目录
		ServletFileUpload fileUpload=new ServletFileUpload(factory);
		List<FileItem> list = fileUpload.parseRequest(request);	
		FileBean bean=new FileBean();
		for(FileItem item:list){
			if(!item.isFormField()){//说明是文件类型
				realPath+=fileNmae+getFileName(item.getName());
				webPath+=getFileName(item.getName());				
				bean.setFileType(item.getContentType());
				bean.setFileSize(item.getSize());
				bean.setRealPath(realPath);
				bean.setWebPath(webPath);
				bean.setOriginName(item.getName());
				bean.setNewName(fileNmae+getFileName(item.getName()));				
				InputStream read=item.getInputStream();
				OutputStream write=new FileOutputStream(realPath);
				byte[] bt = new byte[1024];
				int len=0;
				while((len=read.read(bt))!=-1){
					write.write(bt, 0, len);
				}
				write.flush();
				write.close();
				read.close();
				item.delete();
				break;
			}
		}
		return bean;
	}
	
	/**
	 * 多文件上传
	 * @param request HttpServletRequest
	 * @param directories directories 在 Tomcat webapps 下面的存放文件的文件夹的文件夹名
	 * @return
	 * @throws FileUploadException
	 * @throws IOException
	 */
	public List<FileBean> multiFileUpload(HttpServletRequest request,String directories) throws FileUploadException, IOException{
		request.setCharacterEncoding("UTf-8");
		String realPath=new File(request.getRealPath("/")).getParent()+File.separator+directories+File.separator+randomFilePath();
		String webPath=directories+File.separator+randomFilePath();
		DiskFileItemFactory factory=new DiskFileItemFactory(1024*30,new File(realPath));//创建文件目录
		ServletFileUpload fileUpload=new ServletFileUpload(factory);
		List<FileItem> list = fileUpload.parseRequest(request);	
		List<FileBean> filelist=new ArrayList<FileBean>();
		FileBean bean=null;
		for(FileItem item:list){
			if(!item.isFormField()){//说明是文件类型
				randomFileName();//获取随机文件名
				bean=new FileBean();
				realPath+=fileNmae+getFileName(item.getName());
				webPath+=fileNmae+getFileName(item.getName());				
				bean.setFileType(item.getContentType());
				bean.setFileSize(item.getSize());
				bean.setRealPath(realPath);
				bean.setWebPath(webPath);
				bean.setOriginName(item.getName());
				bean.setNewName(fileNmae+getFileName(item.getName()));				
				InputStream read=item.getInputStream();
				OutputStream write=new FileOutputStream(realPath);
				byte[] bt = new byte[1024];
				int len=0;
				while((len=read.read(bt))!=-1){
					write.write(bt, 0, len);
				}
				write.flush();
				write.close();
				read.close();
				item.delete();
				filelist.add(bean);
			}
		}		
		return filelist;
	}

}

文件上传Bean

package com.xu.utils;

import java.io.Serializable;

public class FileBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8999004458855350561L;
	
	
	private String fileType;
	private String originName;
	private String realPath;
	private String webPath;
	private String newName;
	private Long fileSize;
	public String getFileType() {
		return fileType;
	}
	public void setFileType(String fileType) {
		this.fileType = fileType;
	}
	public String getOriginName() {
		return originName;
	}
	public void setOriginName(String originName) {
		this.originName = originName;
	}
	public String getRealPath() {
		return realPath;
	}
	public void setRealPath(String realPath) {
		this.realPath = realPath;
	}
	public String getWebPath() {
		return webPath;
	}
	public void setWebPath(String webPath) {
		this.webPath = webPath;
	}
	public String getNewName() {
		return newName;
	}
	public void setNewName(String newName) {
		this.newName = newName;
	}
	public Long getFileSize() {
		return fileSize;
	}
	public void setFileSize(Long fileSize) {
		this.fileSize = fileSize;
	}
	@Override
	public String toString() {
		return "FileBean [fileType=" + fileType + ", originName=" + originName + ", realPath=" + realPath
				+ ", webPath=" + webPath + ", newName=" + newName + ", fileSize=" + fileSize + "]";
	}	
}

你可能感兴趣的:(Java,文件上传)