1

我对 common-io-2.4 有疑问,其中 FileItemFactory 似乎没有方法 setReposity。我使用了来自 Apache 网站(Apache File Upload)的代码。这很令人生气,因为我看到其他人使用它并没有任何问题。我已经在我的项目中包含了 jar 文件,并且我没有从库中的其他对象中得到错误,只是那个。就好像它没有得到所有的方法。

有任何想法吗?这是我的代码,我正在用 Netbeans 编写它

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.FileItem;

import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;


/**
 *
 * @author Harry
 */
public class FileUpload extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        
        if(ServletFileUpload.isMultipartContent(request)){ // checks if it is an upload request;
            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();
            
            // Configure a repository (to ensure a secure temp location is used)
            ServletContext servletContext = this.getServletConfig().getServletContext();
            File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
            factory.setReposity(repository);
           

            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            try{
                // Parse the request
               List<FileItem> items = upload.parseRequest(request);  // this creates fileItems, which i then need to loop through and write each one to the file. 
               
               
            }catch(FileUploadException ex){
                System.out.println("Failed to upload file");
            }
            
            
        }
        else{
            response.sendRedirect("UserHome.jsp");
        }
        
    }

这个对象

文件项工厂

似乎没有方法 setReposity 或许多其他方法。我不知道为什么?

也许我只是缺少一些简单的东西?任何帮助都会很棒!干杯!

4

1 回答 1

0

发现示例使用了FileItemFactory,但发现如果切换到DiskFileItemFactory,就可以访问方法。

于 2013-10-31T21:33:58.120 回答