简体   繁体   English

使用Java Servlet上传文件导致找不到资源

[英]Upload file with java servlet results in resource not found

I have a source I think should work but for some reason it gives me resource not found and a completely different resource. 我有一个我认为应该有用的资源,但是由于某种原因,它给了我未找到的资源和完全不同的资源。

HTML part, just simple form: HTML部分,只是简单的形式:

 <html lang="en">
<head>
    <title>File Uploader</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <form method="POST" action="upload" enctype="multipart/form-data" >
        File:
        <input type="file" name="file" id="file" />
        <input type="submit" value="Upload" name="upload" id="upload" />
    </form>
</body>
</html>

Java part: Java部分:

 import java.io.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 import javax.servlet.annotation.*;

 @WebServlet(name = "FileUploader", urlPatterns = "upload")
 @MultipartConfig
 public class FileUploader extends HttpServlet {

 private final static String serverPath = "/fileuploads";

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("text/html;charset=UTF-8");

final Part filePart = request.getPart("file");
String fileName = getFileName(filePart);

OutputStream out = null;
InputStream filecontent = null;
final PrintWriter writer = response.getWriter();

try {
  out = new FileOutputStream(new File(serverPath + File.separator + fileName));
  filecontent = filePart.getInputStream();

  int read = 0;
  final byte[] bytes = new byte[1024];

  while ((read = filecontent.read(bytes)) != -1) {
    out.write(bytes, 0, read);
  }
  writer.println("New file " + fileName + " created at " + serverPath);

} catch (FileNotFoundException fne) {
  writer.println("Missing file or no insufficient permissions.");
  writer.println(" ERROR: " + fne.getMessage());
} finally {
  if (out != null) {
    out.close();
  }
  if (filecontent != null) {
    filecontent.close();
  }
  if (writer != null) {
    writer.close();
  }
}
}

private String getFileName(Part filePart) {
String header = filePart.getHeader("content-disposition");
String name = header.substring(header.indexOf("filename=\"")+10);
return name.substring(0, name.indexOf("\""));
}
}

I would expect the file to be uploaded to /proj/publ/fileuploads but insted it tells me that the resource /proj/publ/uploads is not available.... 我希望文件可以上传到/ proj / publ / fileuploads,但是插入后它告诉我资源/ proj / publ / uploads不可用。

The files are in /proj/publ/ folder. 这些文件位于/ proj / publ /文件夹中。 Why is it always pointing to that folder that does not exist? 为什么总是指向不存在的文件夹?

Thank you for your help. 谢谢您的帮助。

Viking 维京人

EDIT: Prob is solved... for some reason I created the java file in src and not in WEB-INF/src... so there was the problem. 编辑:问题已解决...由于某种原因,我在src中创建了Java文件,而不是在WEB-INF / src中...所以出现了问题。

The resource is not available because the servlet is not mapped to any URL pattern. 该资源不可用,因为该servlet没有映射到任何URL模式。 This can be done either by XML declaration in /WebContent/WEB-INF/web.xml or by @WebServlet annotation. 这可以通过/WebContent/WEB-INF/web.xml中的XML声明或@WebServlet批注来完成。

I see that you already use @WebServlet so you have to add urlPatterns element. 我看到您已经使用@WebServlet,因此必须添加urlPatterns元素。 Something like this: 像这样:

@WebServlet(urlPatterns = "upload", name="FileUploader")

Another thing to solve is the form's action attribute. 要解决的另一件事是表单的action属性。 Based on the servlet container and its configuration the resource won't be available at the root of the container. 基于Servlet容器及其配置,该资源在容器的根目录将不可用。 It usually is in the form of host:port/application-context/url-pattern however this is not a strict rule. 它通常采用host:port / application-context / url-pattern的形式,但这不是严格的规则。 To avoid constant changes what is the right URL you can use JSTL's url tag . 为了避免不断变化,可以使用JSTL的url标记正确的URL。 Something like this: 像这样:

form method="POST" action="<c:url ='/upload' />" enctype="multipart/form-data" >

basically resource not found is when the file we are trying to request is not found or the path of resource is not there. 基本上没有找到资源是在找不到我们要请求的文件或资源路径不存在时。

In your case please declare urlPattern in the webServlet annotation like this. 在您的情况下,请像这样在webServlet批注中声明urlPattern。

@WebServlet(urlPatterns="/upload") @WebServlet(urlPatterns =“ / upload”)

@WebServlet[ https://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebServlet.html] @WebServlet [ https://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebServlet.html]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM