• JavaWeb 文件上传 commons_fileupload方式


    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 javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.File;
    import java.io.IOException;
    import java.util.Iterator;
    import java.util.List;
    
    /**
     * Created with IntelliJ IDEA.
     * User: HYY
     * Date: 13-11-17
     * Time: 下午4:13
     * To change this template use File | Settings | File Templates.
     */
    
    /**
     * @author XHW
     * @date 2011-7-26
     */
    public class FileUploadServlet extends HttpServlet {
    
        private static final long serialVersionUID = -7744625344830285257L;
        private ServletContext sc;
        private String savePath;
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doPost(request, response);
        }
    
    
        public void init(ServletConfig config) {
            //保证根目录下有一个savePath目录
            savePath = "/savePath";
            sc = config.getServletContext();
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            try {
                List items = upload.parseRequest(request);
                Iterator itr = items.iterator();
                while (itr.hasNext()) {
                    FileItem item = (FileItem) itr.next();
                    if (item.isFormField()) {
                        System.out.println("表单参数名:" + item.getFieldName() + ",表单参数值:" + item.getString("UTF-8"));
                    } else {
                        if (item.getName() != null && !item.getName().equals("")) {
                            System.out.println("上传文件的大小:" + item.getSize());
                            System.out.println("上传文件的类型:" + item.getContentType());
                            // item.getName()返回上传文件在客户端的完整路径名称
                            System.out.println("上传文件的名称:" + item.getName());
    
                            File tempFile = new File(item.getName());
    
                            //上传文件的保存路径
                            File file = new File(sc.getRealPath("/") + savePath, tempFile.getName());
                            item.write(file);
                            request.setAttribute("upload.message", "上传文件成功!");
                        } else {
                            request.setAttribute("upload.message", "没有选择上传文件!");
                        }
                    }
                }
            } catch (FileUploadException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
                request.setAttribute("upload.message", "上传文件失败!");
            }
            request.getRequestDispatcher("/uploadResult.jsp").forward(request, response);
        }
    }

    使用该程序时必须保证在根目录下有一个名为savePath文件夹

    至于jsp方面就不写了。

  • 相关阅读:
    0112centos上面l安装卸载mysq
    0111mysql如何选择Join的顺序
    0111MySQL优化的奇技淫巧之STRAIGHT_JOIN
    0108MySQL集群搭建详解(三种结点分离)
    0106主从复制
    0104探究MySQL优化器对索引和JOIN顺序的选择
    MongoDB整理笔记の新增Shard Server
    MongoDB整理笔记の管理Sharding
    MongoDB整理笔记のSharding分片
    MongoDB整理笔记の减少节点
  • 原文地址:https://www.cnblogs.com/wuyou/p/3427951.html
Copyright © 2020-2023  润新知