java-Android到servlet图像上载以保存在服务器上

java-Android到servlet图像上载以保存在服务器上,第1张

概述我已经创建了一个servlet来接受来自我的android应用程序的图像.我正在servlet上接收字节,但是,我希望能够以原始名称将该图像保存在服务器上.我怎么做.我不想使用ApacheCommons.还有其他适合我的解决方案吗?谢谢解决方法:借助Android内置的HttpClientAPI的MultipartEntity类,将其

我已经创建了一个servlet来接受来自我的android应用程序的图像.我正在servlet上接收字节,但是,我希望能够以原始名称将该图像保存在服务器上.我怎么做.我不想使用Apache Commons.还有其他适合我的解决方案吗?

谢谢

解决方法:

借助Android内置的HttpClient API的multipartentity类,将其作为multipart/form-data请求发送.

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

然后在servlet的doPost()方法中,使用Apache Commons FileUpload提取零件.

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

除非您使用的Servlet 3.0支持开箱即用的0033请求多部分/表单数据,否则您将需要根据RFC2388重新创建多部分/表单数据解析器.这只会长期困扰您.硬.我真的看不到您不使用它的任何原因.是无知吗?至少没有那么难.只需将commons-fileupload.jar和commons-io.jar放在/ WEB-INF / lib文件夹中,并使用上面的示例.而已.您可以找到另一个示例here.

总结

以上是内存溢出为你收集整理的java-Android到servlet图像上载以保存在服务器上全部内容,希望文章能够帮你解决java-Android到servlet图像上载以保存在服务器上所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1092835.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存