Issue
I want to upload files on Java Servlet but when i run my upload.jsp and select the files to be uploaded and when i hit submit it shows me HTTP Status 404 – Not Found
upload.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>HELLO WORLD</h2>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" multiple/>
<input type="submit">
</form>
</body>
</html>
FileUpload.java
package com.servlet;
import java.io.*;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.*;
@WebServlet("/FileUpload")
public class FileUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
ServletFileUpload sf = new ServletFileUpload(new DiskFileItemFactory());
List<FileItem> multifiles = sf.parseRequest(request);
for(FileItem item : multifiles) {
item.write(new File("C:/TurboC++/Disk/TurboC3/BIN/Java/Eclipse/Upload-Servlet/" +item.getName()));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
> Sorry for posting whole code but i am unable to solve it.
Solution
Your html form element's action
attribute's value is the URL to the servlet that will receive the http request from submitting your form. Your action
attribute is set to upload
. It appears that you really want it set to /FileUpload
.
Note that setting it to JUST "/FileUpload" may not be sufficient, but you should give it a try. You may need to either specify the whole URL (e.g. http://server.com/context/FileUpload
), or a URL relative to the jsp page (eg /FileUpload
).
Answered By - StvnBrkdll
Answer Checked By - David Goodson (JavaFixing Volunteer)