pack
![](/upload/ad_content/xuanchuantu-1.jpg)
创新互联建站公司2013年成立,先为弓长岭等服务建站,弓长岭等地企业,进行企业商务咨询服务。为弓长岭企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。age com.itheima.servlet;
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.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
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.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import com.itheima.util.IdGenertor;
public class UploadServlet3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//检测form是否是multipart/form-data类型的
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(!isMultipart){
throw new RuntimeException("The form's enctype attribute value must be multipart/form-data");
}
//解析请求内容
DiskFileItemFactory factory = new DiskFileItemFactory();//产生FileItem的工厂
factory.setRepository(new File("d:/"));//指定临时文件的存放目录
ServletFileUpload sfu = new ServletFileUpload(factory);
// sfu.setFileSizeMax(3*1024*1024);//单个文件大小限制
// sfu.setSizeMax(5*1024*1024);//总文件大小
List
items = new ArrayList();
try {
items = sfu.parseRequest(request);
}catch(FileUploadBase.FileSizeLimitExceededException e) {
response.getWriter().write("单个文件不能超过3M");
}
catch(FileUploadBase.SizeLimitExceededException e) {
response.getWriter().write("总文件不能超过5M");
}catch (FileUploadException e) {
e.printStackTrace();
throw new RuntimeException("解析请求失败");
}
//遍历:
for(FileItem item:items){
//处理普通字段
if(item.isFormField()){
processFormField(item);
}else{
//处理上传字段
processUploadField(item);
}
}
}
protected void processUploadField(FileItem item) {
try {
// InputStream in = item.getInputStream();
//找一个存放文件的位置;存放的文件名
String fileName = item.getName();//上传的文件的文件名 C:UserswzhtingDesktopa.txt a.txt(浏览器不同)
if(fileName!=null&&!fileName.equals("")){
//限定上传文件的类型
if(!item.getContentType().startsWith("image")){
return;
}
fileName = FilenameUtils.getName(fileName);
fileName = IdGenertor.genGUID()+"_"+fileName;
//存放路径
String realPath = getServletContext().getRealPath("/WEB-INF/files");
//生成一个子目录
String childDirectory = genChildDirectory(realPath,fileName);
File storeDirectory = new File(realPath+File.separator+childDirectory);
if(!storeDirectory.exists()){
storeDirectory.mkdirs();
}
// OutputStream out = new FileOutputStream(new File(storeDirectory, fileName));
//
// int len = -1;
// byte b[] = new byte[1024];
// while((len=in.read(b))!=-1){
// out.write(b, 0, len);
// }
// in.close();
// out.close();
//
// item.delete();//清除临时文件
item.write(new File(storeDirectory, fileName));
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String genChildDirectory(String realPath, String fileName) {
int hashCode = fileName.hashCode();
int dir1 = hashCode&0xf;
int dir2 = (hashCode&0xf0)>>4;
String str = dir1+File.separator+dir2;
File file = new File(realPath,str);
if(!file.exists()){
file.mkdirs();
}
return str;
}
//按照日期生成子目录
private String genChildDirectory(String realPath) {
Date now = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String str = df.format(now);
File file = new File(realPath,str);
if(!file.exists()){
file.mkdirs();
}
return str;
}
protected void processFormField(FileItem item) {
//打印到控制台
String fieldName = item.getFieldName();
String fieldValue = "";
try {
fieldValue = item.getString("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println(fieldName+"="+fieldValue);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
} 做做笔记,传播知识,我是个勤劳的搬运工
1224144402@qq.com
网页题目:FileUpload组件-创新互联
标题路径:http://bjjierui.cn/article/hjoes.html