java创建httpput请求,文件上传通过HTTP PUT请求

Does anyone have any idea of any products or libraries like Apache Commons FileUpload that will deal with PUT file uploads?

Any friendly advice or direction would be very much appreciated!

Full Story:

We are starting to implement a file upload rest(like) service for our java webapp, but there doesn't seem to be any 'easy' solutions for dealing with file uploads via the HTTP PUT method.

We are hoping to find a library like the Apache Commons FileUpload project, but something that doesn't only deal with "Form-based File Upload in HTML" and/or "multipart/form-data".

We really like FileUpload's ability to store files temporarily, move those files when asked, and then clean up the temporary files after they are no longer used. We also like the fact that Spring will automajically bind the MultipartFile List to our command object and its just available for us to use when it gets into our other html form based file upload controllers.

Full Stack Background:

Spring MVC (3.2.3.RELEASE)

Tomcat 7

We are trying to follow a layered architecture (UI, services/business logic, persistence)

Thank you for your time!

The following url is an example that shows the ability to upload a file from the request's InputStream. The code gets the work done but it isn't quite production quality.

We are using the following curl command to test our webservice:

curl -v -k -X PUT --data-binary @"c:/java/files/tempfilename.txt" https://localhost:8443/api/file/tempfilename.txt

xwoker then gave the following nice curl example:

curl -v -X PUT -T "myfile" http://localhost:8080/mytargetfilename

解决方案

It is quite easy to get spring to respond correctly to a File Upload request for a HTTP PUT Method.

All it takes is overriding the isMultipart() method in a customized MultipartResolver class.

import org.apache.commons.fileupload.FileUploadBase;

import org.apache.commons.fileupload.servlet.ServletRequestContext;

import javax.servlet.http.HttpServletRequest;

public class PostAndPutCommonsMultipartResolver extends CommonsMultipartResolver {

private static final String POST_METHOD = "POST";

private static final String PUT_METHOD = "PUT";

@Override

public boolean isMultipart(HttpServletRequest request) {

boolean isMultipartRequest = false;

if (request != null) {

if (POST_METHOD.equalsIgnoreCase(request.getMethod()) || PUT_METHOD.equalsIgnoreCase(request.getMethod())) {

isMultipartRequest = FileUploadBase.isMultipartContent(new ServletRequestContext(request));

}

}

return isMultipartRequest;

}

}

What is really important is that the default MultipartResolver is extended so that the isMultipart() method will return a true for either a POST or PUT request.

In general there are two default MultipartResolver implementations: CommonsMultipartResolver (used with Apache Commons FileUpload) and StandardServletMultipartResolver (used with Servlet 3.0+ Part API).

Since we are using Apache Commons FileUpload we extended the CommonsMultipartResolver class.

There is documentation on the MultipartResolver's Javadoc page that explains how to properly define a customized MultipartResolver for your application (emphasis is mine):

There is no default resolver implementation used for Spring

DispatcherServlets, as an application might choose to parse its

multipart requests itself. To define an implementation, create a bean

with the id "multipartResolver" in a DispatcherServlet's application

context. Such a resolver gets applied to all requests handled by that

DispatcherServlet.

For an xml configured application it will look close to the following:

For an annotation configured application it will look close to the following:

@Bean(name = "multipartResolver")

public CommonsMultipartResolver createMultipartResolver() {

return new PostAndPutCommonsMultipartResolver();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值