Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving an uploaded image remotely - Java and Tomcat
    primarykey
    data
    text
    <p>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.</p> <p>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 </p> <pre><code>&lt;input type="file" id="file"&gt; </code></pre> <p>and a </p> <pre><code>&lt;input type = "button" onclick = "javaScript: performAjaxSubmit ()"&gt; </code></pre> <p>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.</p> <p>The function is as follows:</p> <pre><code>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); } }; } </code></pre> <p>The code in the Servlet instead is this:</p> <pre><code>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("\\") &gt;= 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); } } </code></pre> <p>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:</p> <pre><code>factory.setRepository(new File("C:/Users/Marty/workspaceJEE/Prova/WebContent/imm/utenti")); </code></pre> <p>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?</p> <p>I hope I explained. I'm using Apache Tomcat v7.0. Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload