gpt4 book ai didi

java - Eclipse JSP "cannot be resolved to a type"Apache 公共(public)资源

转载 作者:行者123 更新时间:2023-11-30 07:37:30 26 4
gpt4 key购买 nike

我尝试从这里复制教程JSP File Uploading 。我正在使用 Eclipse 和 tomcat,但是当我在服务器上运行时出现错误:

 An error occurred at line: 24 in the jsp file: /index.jsp
DiskFileItemFactory cannot be resolved to a type
21: String contentType = request.getContentType();
22: if ((contentType.indexOf("multipart/form-data") >= 0)) {
23:
24: DiskFileItemFactory factory = new DiskFileItemFactory();
25: // maximum size that will be stored in memory
26: factory.setSizeThreshold(maxMemSize);
27: // Location to save data that is larger than maxMemSize.

我已经下载了 commons fileupload 和 io 包,并将这些 jar 添加为构建路径中的外部。该代码是教程的直接副本,包含导入和所有内容,这里是链接中的完整代码。

    <%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.io.output.*" %>

<%
File file ;
int maxFileSize = 5000 * 1024;
int maxMemSize = 5000 * 1024;
ServletContext context = pageContext.getServletContext();
String filePath = context.getInitParameter("file-upload");

// Verify the content type
String contentType = request.getContentType();
if ((contentType.indexOf("multipart/form-data") >= 0)) {

DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Location to save data that is larger than maxMemSize.
factory.setRepository(new File("c:\\temp"));

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
// Parse the request to get file items.
List fileItems = upload.parseRequest(request);

// Process the uploaded file items
Iterator i = fileItems.iterator();

out.println("<html>");
out.println("<head>");
out.println("<title>JSP File upload</title>");
out.println("</head>");
out.println("<body>");
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField () )
{
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filePath +
fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
file = new File( filePath +
fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
out.println("Uploaded Filename: " + filePath +
fileName + "<br>");
}
}
out.println("</body>");
out.println("</html>");
}catch(Exception ex) {
System.out.println(ex);
}
}else{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet upload</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>No file uploaded</p>");
out.println("</body>");
out.println("</html>");
}
%>

最佳答案

您应该将 jar 复制到 WEB-INF/lib。这是保证运行部署的 Web 应用程序时库位于类路径上的唯一方法。

不要在 JSP 文件中编写此文件上传业务逻辑。而不是使用servlet来进行文件上传。您可以找到此类 servlet 的示例 here .

Eclipse 项目结构:

关于java - Eclipse JSP "cannot be resolved to a type"Apache 公共(public)资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35178921/

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