Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Apache commons FileUpload
    primarykey
    data
    text
    <p>This just won't work. The problem is that I do not know enough to even know what is supposed to happen. I can't debug this code. I'd like to store upload to temporary folder "temp" and then to move them to "applets". Please help? The servlet is obviously being accessed, but I can't find the uploaded files... Thanks in advance. </p> <p>Form (which is created using a scriptlet - I put this here if that could cause problems): </p> <pre><code>&lt;% out.write("&lt;p&gt;Upload a new game:&lt;/p&gt;"); out.write("&lt;form name=\"uploadForm\" action=\"game.jsp\" " + "method=\"POST\" enctype=\"multipart/form-data\"&gt;" + "&lt;input type=\"file\" name=\"uploadSelect\" value=\"\" width=\"20\" /&gt;" + "&lt;br&gt;&lt;input type=\"submit\" value=\"Submit\" name=\"uploadSubmitButton\" " + "onclick = \"submitToServlet2('UploadGameServlet');\"&gt;" + "&lt;/form&gt;"); %&gt; </code></pre> <p>Which calls this javascript:</p> <pre><code>function submitToServlet2(newAction) { document.uploadForm.action = newAction; } </code></pre> <p>Which in turn goes to the servlet (code included in full, since there may be some important element hiding)</p> <pre><code>package org.project; import java.io.*; import java.util.Iterator; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; // import servlet stuff import org.apache.commons.fileupload.*; public class UploadGameServlet extends HttpServlet { /** * Processes requests for both HTTP &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); if (ServletFileUpload.isMultipartContent(request)) { try { // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Parse the request List items = upload.parseRequest(request); /* FileItem */ File repositoryPath = new File("\\temp"); DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); diskFileItemFactory.setRepository(repositoryPath); Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); File uploadedFile = new File("\\applets"); item.write(uploadedFile); } } catch (FileUploadException ex) { Logger.getLogger(UploadGameServlet.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(UploadGameServlet.class.getName()).log(Level.SEVERE, null, ex); } } PrintWriter out = response.getWriter(); try { out.println("&lt;html&gt;"); out.println("&lt;head&gt;"); out.println("&lt;title&gt;Servlet UploadGameServlet&lt;/title&gt;"); out.println("&lt;/head&gt;"); out.println("&lt;body&gt;"); out.println("&lt;h1&gt;Servlet UploadGameServlet at " + request.getContextPath () + "&lt;/h1&gt;"); out.println("&lt;/body&gt;"); out.println("&lt;/html&gt;"); } finally { out.close(); } } </code></pre> <p>}</p>
    singulars
    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.
 

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