要的Andr​​oid servlet的图片上传保存在服务器上图片上传、器上、存在、Andr

2023-09-05 04:55:01 作者:情话说的像笑话。

我已经创建了接受来自我的Andr​​oid app.I图像一个servlet我对我的servlet接收的字节,不过,我希望能够挽救这个图像与服务器上的原始名称。我怎么做。我不想使用Apache的百科全书。是否有任何其他的会为我工作的解决方案?

感谢

解决方案

发送其作为的multipart /与 MultipartEntity 类Android的内置的 HttpClient的API 。

  HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httpPost =新HttpPost(http://example.com/uploadservlet);
MultipartEntity实体=新MultipartEntity();
entity.addPart(字段名,新InputStreamBody(fileContent,fileContentType,文件名));
httpPost.setEntity(实体);
HTT presponse ServletResponse的= httpClient.execute(httpPost);
 

然后在servlet的的doPost()的方法,使用阿帕奇通用FileUpload 中提取的部分。

 尝试{
    名单<的FileItem>项目=新ServletFileUpload(新DiskFileItemFactory())的parseRequest(要求)。
    对于(的FileItem项目:项目){
        如果(item.getFieldName()。等于(字段名)){
            字符串文件名= FilenameUtils.getName(item.getName());
            串fileContentType = item.getContentType();
            InputStream的fileContent = item.getInputStream();
            // ...(做你的工作在这里)
        }
    }
}赶上(FileUploadException E){
    抛出新的ServletException异常(无法解析多部分请求。,E);
}
 

  

我不想使用Apache的百科全书的

除非你使用的Servlet 3.0,支持的multipart / form-data的要求用 HttpServletRequest的#的getParts() ,则需要重新塑造一个多部分/格式数据解析器自己的基础上的 RFC2388 。它一定会咬你的长远。硬。我实在看不出有什么理由,你不会使用它。它是普通的无知?它至少没有那么难。刚落公地fileupload.jar 公地io.jar / WEB-INF / lib目录文件夹,然后用上面的例子。而已。你可以找到here另一实例

I have created a servlet that accepts an image from my android app.I am receiving bytes on my servlet, however, I want to be able to save this image with the original name on the server. How do I do that. I dont want to use apache commons. Is there any other solution that would work for me?

thanks

解决方案

Send it as a multipart/form-data request with help of MultipartEntity class of Android's builtin 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 servlet's doPost() method, 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 dont want to use apache commons

Unless you're using Servlet 3.0 which supports multipart/form-data request out the box with HttpServletRequest#getParts(), you would need to reinvent a multipart/form-data parser yourself based on RFC2388. 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 commons-fileupload.jar and commons-io.jar in /WEB-INF/lib folder and use the above example. That's it. You can find here another example.