Java 中的文件上传

这位博主已经写得很好了:Java 中的文件上传类

我这里就贴上我的代码

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html; charset=utf-8");


    boolean isMultipart=ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("utf-8");

        //            upload.setProgressListener(new ProgressListener(){
        //                public void update(long pBytesRead, long pContentLength, int pItems) {
        //                    ProcessInfo pri = new ProcessInfo();
        //                    pri.itemNum = pItems;
        //                    pri.readSize = pBytesRead;
        //                    pri.totalSize = pContentLength;
        //                    pri.show = pBytesRead+"/"+pContentLength+" byte";
        //                    pri.rate = Math.round(new Float(pBytesRead) / new Float(pContentLength)*100);
        //                    hs.setAttribute("proInfo", pri);
        //                }
        //            });

        try {
            List<FileItem> items = upload.parseRequest(request);
            Iterator<FileItem> it = items.iterator();
            while (it.hasNext()) {
                FileItem item = it.next();
                String itemname = item.getFieldName();
                int sno = -1;
                String sname = null;

                if (item.isFormField()) {
                    if (itemname.equals("sno")) {
                        sno = Integer.parseInt(item.getString("utf-8"));
                    } else if (itemname.equals("sname")) {
                        sname = item.getString("utf-8");
                        sname = item.getName();
                    } else {
                        System.out.println("其他字段");
                    }
                } else {
                    String filename = item.getName();
                    //String path=request.getSession().getServletContext().getRealPath("upload");
                    String path = this.getServletContext().getRealPath("upload");

                    System.out.println(path);
                    File file = new File(path, filename);
                    if (!file.exists() && !file.isDirectory()) {
                        //                            file.mkdir();
                        file.getParentFile().mkdirs(); // 文件不存在则创建新文件
                    }
                    item.write(file);
                    request.setAttribute("news", filename + "  上传成功!");
                    request.getRequestDispatcher("/WEB-INF/admin/uploadFile.jsp").forward(request, response);
                    //                        response.sendRedirect("fileUploadIndexServlet");
                    System.out.println(filename + "上传成功!!!");

                    return;
                }

            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
posted @ 2021-03-29 20:09  绯狱丸丶  阅读(62)  评论(0编辑  收藏  举报