[英]Get data form HTTP POST request with multipart/form-data in Java (Spring)
I want upload file from flash, my raw request (cached with Fiddler): 我想从Flash上传文件,这是我的原始请求(已用Fiddler缓存):
POST /api/v1/exercises/uploadTableImage HTTP/1.1
x-flash-version: 11,2,202,228
Content-Type: multipart/form-data; boundary=jdmlcuucuseqxyyyvsfjbfixukdbuesq
Cache-Control: no-cache
Content-Length: 93737
User-Agent: Shockwave Flash
Host: localhost:8080
Pragma: no-cache
Cookie: JSESSIONID=BA009CDDBD828B931FCC3B0894FD7DCD;
--jdmlcuucuseqxyyyvsfjbfixukdbuesq
Content-Disposition: form-data; name="Filename"
20er_1_1.jpg.jpg
--jdmlcuucuseqxyyyvsfjbfixukdbuesq
Content-Disposition: form-data; name="filedata"; filename="20er_1_1.jpg.jpg"
Content-Type: application/octet-stream
1
--jdmlcuucuseqxyyyvsfjbfixukdbuesq
Content-Disposition: form-data; name="Upload"
Submit Query
--jdmlcuucuseqxyyyvsfjbfixukdbuesq--
I want save posted file 我要保存发布的文件
@Controller
@RequestMapping(API_ROOT+"exercises")
public class ImageUploadingController {
private final String imagesWebPath = "uploaded";
@RequestMapping(value = "uploadTableImage", method = POST)
public void uploadImage(HttpServletRequest request) throws Exception {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
FileItem uploadedFile = null;
List<FileItem> items = upload.parseRequest(request);
for (FileItem item : items) {
if ("Filedata".equals(item.getFieldName())) {
uploadedFile = item;
break;
}
}
if (uploadedFile != null) {
File file = new File(request.getRealPath(imagesWebPath)+File.separator+uploadedFile.getName());
file.getParentFile().mkdirs();
file.createNewFile();
uploadedFile.write(file);
} else {
throw new RuntimeException("No files found");
}
}
But no luck, I see no items in List items = upload.parseRequest(request);. 但没有运气,我没有看到List items = upload.parseRequest(request);中的任何项目。 Due to requirements I cannot change request headers. 由于要求,我无法更改请求标头。
Solved! 解决了! Problem was in spring configuration xml file. 问题出在spring配置xml文件中。 From previous implementation left 从之前的实现中
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000"/> </bean>
So spring read HttpRequest before my method and 所以春天在我的方法之前读了HttpRequest和
upload.parseRequest(request)
read empty stream. 读取空流。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.