java - adding text input to multipart/form-data makes it fail -


this question has answer here:

so want t simple form transfer text , image. now, can't both @ same time !

here simple form code:

<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!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>file upload</title> </head> <body> <center> <h1>file upload</h1> <form action="uploadservlet" method="post" enctype="multipart/form-data"> <input type="text" name="captionbox" /> <input type="file" name="photo" /> <input type="submit" /> </form> </center> </body> </html> 

this code give me error:

java.io.ioexception: java.io.filenotfoundexception: c:\users\poste hp\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\maroc_events\uploadfiles (access denied)  

but if put away text input , let file input, works! , can't figure out why !

here servlet code:

@webservlet("/uploadservlet") @multipartconfig public class uploadservlet extends httpservlet{  /**  * name of directory uploaded files saved, relative  * web application directory.  */ private static final string save_dir = "uploadfiles";  /**  * handles file upload  */ protected void dopost(httpservletrequest request,     httpservletresponse response) throws servletexception, ioexception {     eventutil.addimage(request);     request.setattribute("message", "upload has been done successfully!");     getservletcontext().getrequestdispatcher("/message.jsp").forward(             request, response); } 

}

and eventutil class:

package com.utils;  import java.io.file; import java.io.ioexception;  import javax.servlet.servletexception; import javax.servlet.http.httpservletrequest; import javax.servlet.http.part;  public class eventutil {  private static final string save_dir = "uploadfiles";  public static void addimage(httpservletrequest request) throws ioexception, servletexception{     string apppath = request.getservletcontext().getrealpath("");     // constructs path of directory save uploaded file     string savepath = apppath + file.separator + save_dir;     system.out.println(savepath);      // creates save directory if not exists     file filesavedir = new file(savepath);     if (!filesavedir.exists()) {         filesavedir.mkdir();     }      (part part : request.getparts()) {         string filename = extractfilename(part);         part.write(savepath + file.separator + filename);     } }  private static string extractfilename(part part) {     string contentdisp = part.getheader("content-disposition");     string[] items = contentdisp.split(";");     (string s : items) {         if (s.trim().startswith("filename")) {             return s.substring(s.indexof("=") + 2, s.length()-1);         }     }     return ""; }  } 

thanks !

edit: stacktrace:

java.io.filenotfoundexception: c:\users\poste hp\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\maroc_events\uploadfiles (accès refusé) java.io.fileoutputstream.open(native method) java.io.fileoutputstream.<init>(unknown source) java.io.fileoutputstream.<init>(unknown source) org.apache.tomcat.util.http.fileupload.disk.diskfileitem.write(diskfileitem.java:394) com.utils.eventutil.addimage(eventutil.java:28) com.servlets.uploadservlet.dopost(uploadservlet.java:54) javax.servlet.http.httpservlet.service(httpservlet.java:648) javax.servlet.http.httpservlet.service(httpservlet.java:729) org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) 

the write() method throws error because ...\maro‌​c_events\uploadfiles directory, , cannot write directory.

you can write file in directory, require filename. likely, filename blank.

not sure, believe getparts() return non-file parts, means returns part captionbox field. try printing part.getname() check.

since expect 1 file anyway, use request.getpart("photo") instead of looping.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -