Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code line <code>List fileItems = upload.parseRequest(request);</code> and even all the lines in the <code>else</code> part doesn't have any sense at all because:</p> <ol> <li><p>You already have processed the request before in this line</p> <pre><code>List&lt;FileItem&gt; items = new ServletFileUpload( new DiskFileItemFactory()).parseRequest(request); </code></pre> <p>No need to process the request again.</p></li> <li><p>You already have the file you want/need to process in the <code>item</code> object which <code>isFormField</code> method returns false.</p></li> </ol> <p>Change your method in order to look like here: <a href="https://stackoverflow.com/a/2424824/1065197">How to upload files to server using JSP/Servlet?</a>:</p> <pre><code>FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List&lt;FileItem&gt; items = upload.parseRequest(request); for (FileItem item : items) { if (item.isFormField()) { //als het een veld is dan dit, anders File uploaden String fieldname = item.getFieldName(); String fieldvalue = item.getString(); switch (fieldname) { case "logicalName": LogicalName = fieldvalue; break; //other case statements... } } else { //here you only have to process the file File file; int maxFileSize = 500000 * 1024;//your comments... int maxMemSize = 5000 * 1024;//your comments... //this must be a constant or a servlet init param, do not hard code it String filePath = "C:\\uploads\\"; String fileName = FilenameUtils.getName(item.getName()); factory.setSizeThreshold(maxMemSize); //didn't you have a filePath variable? factory.setRepository(new File("c:\\temp")); upload.setSizeMax(maxFileSize); try { String fieldName = fi.getFieldName(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); file = new File(filePath, fileName); item.write(file); //code to save your file location in db... //note: this MUST BE in a business logic method, not directly written in your servlet HttpSession session = request.getSession(); int uploader = (Integer) session.getAttribute("UserId"); } catch (Exception ex) { //very BAD idea //use a logger instead like log4j or sfl4j System.out.println(ex); } } } </code></pre> <p>Additional: check the <a href="http://docs.oracle.com/javase/6/docs/api/java/io/File.html#File%28java.lang.String,%20java.lang.String%29" rel="nofollow noreferrer">File(String parent, String child)</a> constructor.</p>
 

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