我正在尝试将图片发送到我的java servlet(托管在amazon ec2上)以便稍后将其传输到amazon s3,并想知道如何从post请求中检索图像。
请求通过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"));
问题是:如何在服务器上检索和解析图像?
答案 0 :(得分:4)
Servlet 3.0
支持阅读多部分数据。 Servlet 3.0
中的MutlipartConfig支持如果使用@MutlipartConfig
注释对servelt进行注释,则容器负责通过
HttpServletRequest.getParts()
HttpServletRequest.getPart("name");
<强>参考文献:强>
答案 1 :(得分: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();
}
}
}
请参阅FileItem
API参考文档以确定下一步操作。