I'm creating a JSP/servlet based web app that uploads a file to a folder on the server.
But when I hit the link to the uploaded file, I'm not able to access it.
E.g. I have uploaded the file at /Projects/MyFiles/file.html
when I try to access that file using
localhost:8083/Projects/MyFiles/file.html
it doesn't work.
Can anyone please help?
here is my code
@WebServlet("/uploadIDCM")
public class Servlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String finalString = "Yet to";
if(ServletFileUpload.isMultipartContent(request)){
try {
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for(FileItem item : multiparts){
if(!item.isFormField()){
String name = new File(item.getName()).getName();
item.write( new File("/Projects/MyFiles/" + File.separator + name));
}
}
finalString = "Success";
request.setAttribute("msg", "File Uploaded Successfully");
} catch (Exception ex) {
finalString = "Exception" + ex.toString();
request.setAttribute("msg", "File Upload Failed due to " + ex);
}
}else{
finalString = "No file found";
request.setAttribute("msg","No File found");
}
}
}
index.js file is
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>IDCM Builds</title>
</head>
<body>
<Center>
<P>Upload the iOS or Android file(s) here. Upload one by one, or all at once of a specific type</P>
<P>For .html and plist files, destination folder is determined by the File Type option you use below</P>
<P><B>Note:</B> .html, .plist, .ipa or .apk extensions only accepted as valid files</P>
<form action="uploadIDCM" method="post" enctype="multipart/form-data">
<table border=1><tr><Td><label for="type">File(s) Type :</label></td>
<td><select name="ftype">
<option value="iOS_Build">iOS_Build</option>
<option value="Android_Build">Android_Build</option>
</select></td></tr>
<tr><Td><label for="file">Upload File(s):</label></Td>
<Td><input type="file" name="file[]" multiple ></Td></tr>
</table>
<input name="submit" type="submit" value="Upload" />
</Center>
</form>
</body>
</html>