These are days that I’m banging my head on this problem and maybe you that certainly know more than me you can help me ….
Then I try to explain better.
I have a javascript file that through the library d3.js builds the html code pages and replaces it with the other code each part a different function … The page will not charge (Ajax).
At some point I need to allow the user to upload an image to their profile picture so I make sure that the html code bait
<input type="file" id="file">
and a
<input type = "button" onclick = "javaScript: performAjaxSubmit ()">
PerformAjaxSubmit function () sends the data to a Java Servlet via a xmlHttpRequest level 2, which, from what I understand, can send not only strings but also more complex things such as files.
The function is as follows:
function performAjaxSubmit() {
var sampleFile = document.getElementById("file").files[0];
var formdata = new FormData();
formdata.append("sampleFile", sampleFile);
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://127.0.0.1:8080/Prova/Upload", true);
xhr.send(formdata);
xhr.onload = function(e) {
if (this.status == 200) {
alert(this.responseText);
}
};
}
The code in the Servlet instead is this:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Check that we have a file upload request
System.out.println(request.getAttribute("username"));
isMultipart = ServletFileUpload.isMultipartContent(request);
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter( );
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:/Users/Marty/workspaceJEE/Prova/WebContent/imm/utenti"));
filePath="C:/Users/Marty/workspaceJEE/Prova/WebContent/imm/utenti";
// 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();
while ( i.hasNext () )
{
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField () )
{
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
String contentType = fi.getContentType();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filePath +"/"+
fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
System.out.println(filePath +
fileName.substring(fileName.lastIndexOf("\\")+1));
file = new File( filePath +"/"+
fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
}
}
}catch(Exception ex) {
System.out.println(ex);
}
}
Now (sorry if the question is a bit long) it works but the problem is that the images are saved in the path that I have provided me with the command:
factory.setRepository(new File("C:/Users/Marty/workspaceJEE/Prova/WebContent/imm/utenti"));
How do I then save it remotely? That is, once I load the site of such Altrevista, how do I make sure that they are not pià saved to C but in a folder in your project?
I hope I explained. I’m using Apache Tomcat v7.0.
Thanks in advance!
You can use ServletContext.getRealPath
This code returns
<context root>/upload(depends on your deployment configuration)