Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Send it as a <a href="http://www.faqs.org/rfcs/rfc2388.html" rel="nofollow noreferrer">multipart/form-data</a> request with help of <code>MultipartEntity</code> class of Android's builtin <a href="http://developer.android.com/reference/org/apache/http/client/HttpClient.html" rel="nofollow noreferrer">HttpClient API</a>.</p> <pre><code>HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://example.com/uploadservlet"); MultipartEntity entity = new MultipartEntity(); entity.addPart("fieldname", new InputStreamBody(fileContent, fileContentType, fileName)); httpPost.setEntity(entity); HttpResponse servletResponse = httpClient.execute(httpPost); </code></pre> <p>And then in servlet's <code>doPost()</code> method, use <a href="http://commons.apache.org/fileupload" rel="nofollow noreferrer">Apache Commons FileUpload</a> to extract the part.</p> <pre><code>try { List&lt;FileItem&gt; items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); for (FileItem item : items) { if (item.getFieldName().equals("fieldname")) { String fileName = FilenameUtils.getName(item.getName()); String fileContentType = item.getContentType(); InputStream fileContent = item.getInputStream(); // ... (do your job here) } } } catch (FileUploadException e) { throw new ServletException("Cannot parse multipart request.", e); } </code></pre> <hr> <blockquote> <p><em>I dont want to use apache commons</em></p> </blockquote> <p>Unless you're using Servlet 3.0 which supports <code>multipart/form-data</code> request out the box with <a href="http://download.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getParts()" rel="nofollow noreferrer"><code>HttpServletRequest#getParts()</code></a>, you would need to reinvent a multipart/form-data parser yourself based on <a href="http://www.faqs.org/rfcs/rfc2388.html" rel="nofollow noreferrer">RFC2388</a>. It's only going to bite you on long term. Hard. I really don't see any reasons why you wouldn't use it. Is it plain ignorance? It's at least not that hard. Just drop <code>commons-fileupload.jar</code> and <code>commons-io.jar</code> in <code>/WEB-INF/lib</code> folder and use the above example. That's it. You can find <a href="https://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824">here</a> another example.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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