Download Android servlet to save to server

I created a servlet that takes an image from my android application. I get bytes on my servlet, however I want to save this image with the original name on the server. How to do it. I do not want to use apache. Is there any other solution that will work for me?

thank

+3
source share
1 answer

Send it as a multipart / form-data request using MultipartEntitythe Android builtin class HttpClient API .

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);

And then in the servlet method doPost()use Apache Commons FileUpload to extract the part.

try {
    List<FileItem> 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);
}

I do not want to use apache commons

Servlet 3.0, multipart/form-data HttpServletRequest#getParts(), multipart/form-data parser RFC2388. . . , . ? . commons-fileupload.jar commons-io.jar /WEB-INF/lib . . .

+5

Source: https://habr.com/ru/post/1796518/


All Articles