Http 415 on file Upload using jersey(使用球衣上传文件上的 Http 415)
问题描述
我的 RESTful 文件上传代码:
My code for RESTful file upload :
@Path("/upload")
@POST
@Consumes("multipart/form-data")
public String post(
@FormDataParam("part") String s,
@FormDataParam("part") FormDataContentDisposition d) {
return s + ":" + d.getFileName();
}
当我尝试使用 curl 上传文件时curl -X POST --form part=@file.txt url
When I try to upload a file using curl curl -X POST --form part=@file.txt url
我收到 HTTP 415-不支持的媒体类型错误.怎么了?
I am getting a HTTP 415-Unsupported Media Type Error. What is wrong ?
推荐答案
在尝试了很多示例之后,最终在 http://iambigd.blogspot.com/2011/06/java-upload-file-using-jersey.html
After trying a lot of examples finaly find the realy working example on http://iambigd.blogspot.com/2011/06/java-upload-file-using-jersey.html
@POST
@Path("/simpleupload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void simpleUpload(
//@Context UriInfo ui,
@Context HttpServletRequest request
){
String fileRepository = "D:\";
if (ServletFileUpload.isMultipartContent(request)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
if (items != null) {
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (!item.isFormField() && item.getSize() > 0) {
System.out.println("File is found.");
String fileName = processFileName(item.getName());
try {
String savePath = fileRepository + fileName;
System.out.println("savePath:" + savePath);
item.write(new File(savePath));
} catch (Exception e) {
e.printStackTrace();
}
}else{
System.out.println("getFieldName:" + item.getFieldName());
System.out.println(item.getString());
}
}
}
}
}
(需要servlet-api.jar、(apache)commons-oi.jar和(apache)commons-fileupload.jar)
(need the servlet-api.jar, (apache) commons-oi.jar and (apache) commons-fileupload.jar)
这篇关于使用球衣上传文件上的 Http 415的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用球衣上传文件上的 Http 415
基础教程推荐
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 double 转换为 Int,向下舍入 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
