- package zhiwei.deng.web.servlet.util;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- 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.disk.DiskFileItemFactory;
- import org.apache.commons.fileupload.servlet.ServletFileUpload;
- import zhiwei.deng.web.servlet.domain.User;
- import zhiwei.deng.web.servlet.exception.NoFileUpException;
- import zhiwei.deng.web.servlet.exception.UpFileSizeException;
- import zhiwei.deng.web.servlet.exception.UpFileTypeException;
- public class UploadUtil {
- //设置文件上传的临时目录
- public static String tempPath = "/WEB-INF/temp";
- public static String uploadPath = "/WEB-INF/upload";
-
-
-
-
- //
- public static void doSave(User user,String uploadPath) throws Exception{
- //得到上传的文件
- List<FileItem> fileItemList = user.getFileItemList();
- for(FileItem fileItem : fileItemList){
- BufferedInputStream bis = new BufferedInputStream(fileItem.getInputStream());
- //取得uuid文件名
- String realFileName = fileItem.getName();
- realFileName = UploadUtil.realFileName(realFileName);
- String uuidFileName = UploadUtil.makeUuidFileName(realFileName);
- //取得uuid文件路径
- String uuidFilePath = UploadUtil.makeUuidFilePath(uploadPath, realFileName);
- //复制
- UploadUtil.doSave(bis, uuidFilePath, uuidFileName);
- //删除临时目录
- fileItem.delete();
- }
-
- }
-
-
- //将上传文件封装到一个javabean中
- public static User doUpload(HttpServletRequest request) throws Exception{
-
- User user = new User();
- //得到一个文件上传的工厂
- DiskFileItemFactory factory = new DiskFileItemFactory();
- //设置缓存的大小,默认是10K
- factory.setSizeThreshold(100*1024);
-
-
- String tempPath = request.getSession().getServletContext().getRealPath(UploadUtil.tempPath);
- factory.setRepository(new File(tempPath));
-
- //文件上传的核心
- ServletFileUpload upload = new ServletFileUpload(factory);
-
- //设置文件上传的编码方式
- upload.setHeaderEncoding("utf-8");
- //判断上传的是否是MIME协议
- boolean flag = upload.isMultipartContent(request);
- if(flag){
- //如果是MIME协议的话
- List<FileItem> list = upload.parseRequest(request);
- List<FileItem> fileItemList = new ArrayList<FileItem>();
- for(FileItem fileItem : list){
- //判断是否是普通(窗体)字段,只有两种情况,要不然就是上传字段
- if(fileItem.isFormField()){
- //如果是普通字段
- String fieldName = fileItem.getFieldName();
- String fieldValue = fileItem.getString("utf-8");
- //把用户的名称放进javaBean中
- user.setUsername(fieldValue);
- }else{
- //必定是上传字段了
-
- //没有上传文件
- if(fileItem.getSize()==0){
- throw new NoFileUpException();
- }
- if(!UploadUtil.realFileName(fileItem.getName()).endsWith(".jpg")){
- fileItem.delete();
- throw new UpFileTypeException();
- }
- if(fileItem.getSize()> 200 * 1024){
- fileItem.delete();
- throw new UpFileSizeException();
- }
- //user.setFileItem(fileItem);
- //fileItemList.add(fileItem);
- user.getFileItemList().add(fileItem);
- }
- }
- }else{
- throw new ServletException();
- }
- return user;
- }
-
-
-
- //复制文件
- public static void doSave(BufferedInputStream bis,String uuidFilePath,String uuidFileName){
- BufferedOutputStream bos = null;
- try {
- bos = new BufferedOutputStream(new FileOutputStream(uuidFilePath+"/"+uuidFileName));
- int len=0;
- byte[] buf = new byte[1024];
- while((len=bis.read())>0){
- bos.write(buf, 0, len);
- }
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- if(bis!=null){
- try {
- bis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(bos!=null){
- try {
- bos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
- }
-
-
-
-
-
- //获取真实路径
- public static String realFileName(String realFileName){
- int index = realFileName.lastIndexOf("\\");
- if(index>=0){
- realFileName = realFileName.substring(index+1);
- }
- return realFileName;
- }
-
-
-
-
-
-
- //设置得到uuid的文件路径
- public static String makeUuidFilePath(String uploadPath, String uuidFileName) {
- String uuidFilePath = null;
- int code = uuidFileName.hashCode();//打印文件名的hashcode码,
- int dir1 = code & 0xF;//设置一级目录
- int dir2 = code>>4 & 0xF;
- File file = new File(uploadPath+"/"+dir1+"/"+dir2);
- if(!file.exists()){
- //如果目录不存在,一次性创建
- file.mkdirs();
- }
- uuidFilePath = file.getPath();
-
- return uuidFilePath;
- }
- //设置得到uuId的文件名称
- public static String makeUuidFileName(String realFileName) {
- return UUID.randomUUID().toString()+"_"+realFileName;
- }
-
- }
复制代码- package zhiwei.deng.web.servlet.upload;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.List;
- import java.util.UUID;
- 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;
- import zhiwei.deng.web.servlet.domain.User;
- import zhiwei.deng.web.servlet.exception.NoFileUpException;
- import zhiwei.deng.web.servlet.exception.UpFileSizeException;
- import zhiwei.deng.web.servlet.exception.UpFileTypeException;
- import zhiwei.deng.web.servlet.util.UploadUtil;
- public class UploadServlet extends HttpServlet {
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- try {
- User user = UploadUtil.doUpload(request);
- //得到上传的临时路径
- String uploadPath = this.getServletContext().getRealPath(UploadUtil.uploadPath);
- UploadUtil.doSave(user, uploadPath);
-
- request.setAttribute("message", "上传成功");
- request.getRequestDispatcher("/WEB-INF/message.jsp").forward(request, response);
- }catch(UpFileSizeException e){
- request.setAttribute("message", "上传的文件的大小超过200K");
- request.getRequestDispatcher("/WEB-INF/message.jsp").forward(request, response);
- }catch(UpFileTypeException e){
- request.setAttribute("message","上传文件格式不对,只能为JPG格式的");
- request.getRequestDispatcher("/WEB-INF/message.jsp").forward(request, response);
- }catch(NoFileUpException e){
- request.setAttribute("message", "没有上传文件");
- request.getRequestDispatcher("/WEB-INF/message.jsp").forward(request, response);
- }catch (Exception e) {
- e.printStackTrace();
- request.setAttribute("message","上传失败");
- request.getRequestDispatcher("/WEB-INF/message.jsp").forward(request, response);
- }
- }
- }
复制代码 |