Java – HTTP 415 on file upload using Jersey
•
Java
My restful file upload code:
@Path("/upload") @POST @Consumes("multipart/form-data") public String post( @FormDataParam("part") String s,@FormDataParam("part") FormDataContentDisposition d) { return s + ":" + d.getFileName(); }
When I try to upload a file using curl, curl - x post – means part = @ file Txt URL
I received a media type error that HTTP 415 does not support What's up?
Solution
After trying many examples, finally http://iambigd.blogspot.com/2011/06/java-upload-file-using-jersey.html Found a really effective example
@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 is required, (APACHE) commons OI Jar and (APACHE) commons - fileUpload jar)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码