zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    
  • 相关阅读:
    第四次实验报告
    第三次实验报告
    第五章 循环结构课后反思
    第二次实验报告
    5-508寝室第六小组课后习题作业
    第一次实验报告
    第九章 构造数据类型实验
    第八章 指针实验
    第七章 数组实验
    第六章 函数和宏定义实验(2)
  • 原文地址:https://www.cnblogs.com/Constantin/p/14594030.html
Copyright © 2011-2022 走看看