WEB开发网
开发学院软件开发Java Struts2文件上传(一):Common-FileUpload 阅读

Struts2文件上传(一):Common-FileUpload

 2009-09-18 00:00:00 来源:WEB开发网   
核心提示: UploadServletJava代码 protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{try{DiskFileItemFac

UploadServlet

Java代码

protected void doPost(HttpServletRequest request,  
      HttpServletResponse response) throws ServletException, IOException {  
    try {  
      DiskFileItemFactory factory = new DiskFileItemFactory();  
      // 设置临时缓存大小,文件大于这个值的部分保存到临时目录  
      factory.setSizeThreshold(1024 * 128);  
      //设置文件保存临时目录  
      factory.setRepository(new File(  
          "C:\\Documents and Settings\\bond\\temp"));  
      ServletFileUpload upload = new ServletFileUpload(factory);  
      // 设置上传内容大小  
      upload.setSizeMax(1024 * 1024*512);  
  
      List fileItems = upload.parseRequest(request);  
      for (FileItem fi : fileItems) {  
        if (!fi.isFormField()) {  
          fi  
              .write(new File("D:\\bond\\upload", fi  
                  .getName()));  
        }  
      }  
      request.getRequestDispatcher("success.jsp").forward(request,  
          response);  
    } catch (Exception e) {  
      e.printStackTrace();  
      request.getRequestDispatcher("failure.jsp").forward(request,  
          response);  
    } 

上一页  1 2 3 

Tags:Struts 文件 上传

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接