5

我正在尝试将图片发送到我的 java servlet(托管在亚马逊 ec2 上),以便稍后将其传输到亚马逊 s3,并想知道如何从发布请求中检索图像。

上传代码

请求通过 iOS RestKit API 发送,如下所示(pic.imageData 是 NSData 类型):

RKParams* params = [RKParams params];

[params setValue:pic.dateTaken forParam:@"dateTaken"];
[params setValue:pic.dateUploaded forParam:@"dateUploaded"];

[params setData:pic.imageData MIMEType:@"image/jpeg" forParam:@"image"];

[RKClient sharedClient].username = deviceID;
[RKClient sharedClient].password = sessionKey;

[RKClient sharedClient].authenticationType = RKRequestAuthenticationTypeHTTPBasic;

uploadPictureRequest = [[RKClient sharedClient] post:kUploadPictureServlet params:params delegate:self];

解析代码存根

这就是我在 Java servlet 上解析其他 2 个参数的方式:

double dateTaken = Double.parseDouble(req.getParameter("dateTaken"));
double dateUploaded = Double.parseDouble(req.getParameter("dateUploaded"));

问题

问题是:如何检索和解析服务器上的图像?

4

2 回答 2

4

Servlet 3.0支持读取多部分数据。MutlipartConfig 支持Servlet 3.0如果使用注解对一个servelt进行@MutlipartConfig注解,则容器负责通过以下方式使 Multipart 部分可用

HttpServletRequest.getParts()
HttpServletRequest.getPart("name");

参考:

于 2012-07-12T05:09:41.453 回答
3

与此类似,使用Apache Commons FileUpload

 // or @SuppressWarnings("unchecked")
 @SuppressWarnings("rawtypes")
 public void doPost(HttpServletRequest request, HttpServletResponse response) 
         throws ServletException, IOException {
     if (ServletFileUpload.isMultipartContent(request)) {
         final FileItemFactory   factory = new DiskFileItemFactory();
         final ServletFileUpload upload  = new ServletFileUpload(factory);

         try {
             final List items = upload.parseRequest(request);

             for (Iterator itr = items.iterator(); itr.hasNext();) {
                 final FileItem item = (FileItem) itr.next();

                 if (!item.isFormField()) {
                    /*
                     * TODO: (for you)
                     *  1. Verify that file item is an image type.
                     *  2. And do whatever you want with it.
                     */
                 }
             }
         } catch (FileUploadException e) {
             e.printStackTrace();
         }
     }
 }

请参阅FileItemAPI 参考文档以确定下一步要做什么。

于 2012-07-12T03:30:54.860 回答