gpt4 book ai didi

java - JSP Java io.common 上传文件问题

转载 作者:行者123 更新时间:2023-12-01 04:13:05 24 4
gpt4 key购买 nike

我在 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 方法或许多其他方法。不知道为什么?

也许我只是错过了一些简单的事情?任何帮助都会很棒!干杯!

最佳答案

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

关于java - JSP Java io.common 上传文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19715761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com