SpringMVC-实现PUT请求上传文件(转)
因为在图片上传的时候使用的是二进制的方式上传,所以使用隐藏域进行方法转换方式失效,转方法:
https://www.cnblogs.com/morethink/p/6378015.html
可是后来我又有遇到另外一个需求那就是修改的时候需要传送文件到put方法中,于是这种方法就不可行了,但是我在HiddenHttpMethodFilter源码中看到这样一句话
* <p><b>NOTE: This filter needs to run after multipart processing in case of a multipart
* POST request, due to its inherent need for checking a POST body parameter.</b>
* So typically, put a Spring {@link org.springframework.web.multipart.support.MultipartFilter}
* <i>before</i> this HiddenHttpMethodFilter in your {@code web.xml} filter chain.
和MultipartFilter源码中这样的注释
/**
* Set the bean name of the MultipartResolver to fetch from Spring's
* root application context. Default is "filterMultipartResolver".
*/
也就是说我们可以通过在web.xml中注册一个MultipartFilter,一定要在HiddenHttpMethodFilter之前。
<filter>
<filter-name>MultipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MultipartFilter</filter-name>
<servlet-name>dispatcher</servlet-name>
</filter-mapping> <filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>然后再在Spring的 root application context中添加如下代码
<bean id="filterMultipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="209715200"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="resolveLazily" value="true"/>
</bean>FormData对象是html5的一个对象,目前的一些主流的浏览器都已经兼容。FormData对象是html5的一个对象,目前的一些主流的浏览器都已经兼容。
function test() {
var form = new FormData(document.getElementById("tf"));
form.append("_method", 'put');
$.ajax({
url: url,
type: 'post',
data: form,
processData: false,
contentType: false,
success: function (data) {
window.clearInterval(timer);
console.log("over..");
},
error: function (e) {
alert("错误!!");
window.clearInterval(timer);
}
});
get();//此处为上传文件的进度条
}
<form id="tf" method="post" name="formDada" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="text" name="id"/>
<input type="text" name="name"/>
<input type="button" value="提" onclick="test()"/>
</form>
最后,就可以实现将文件上传提交给put方法。
我使用文章中的方法还是失败,修改web.xml中multipartfilter的mapping之后成功
<filter>
<filter-name>multipartFilter</filter-name>
<filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
<init-param>
<param-name>multipartResolverBeanName</param-name>
<param-value>multipartResolver</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>multipartFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
SpringMVC-实现PUT请求上传文件(转)的更多相关文章
- SpringMVC实现PUT请求上传文件
在JQuery中,我们可以进行REST ful中delete和put的请求,但是在java EE标准中,默认只有在POST请求的时候,servlet 才会通过getparameter()方法取得请求体 ...
- SSM框架下,使用ajax请求上传文件(doc\docx\excel\图片等)
1.准备工作 1.1.添加上传必要jar包 <dependency> <groupId>commons-io</groupId> <artifactId> ...
- Postman Post请求上传文件
Postman Post请求上传文件一.选择post请求方式,输入请求地址 二.填写Headers Key:Content-Type :Value:multipart/form-data 如下图 三. ...
- python中使用multipart/form-data请求上传文件
最近测试的接口是上传文件的接口,上传单个文件,我主要使用了2种方法~ 接口例如: URL: http://www.baidu.com/*** method:post 参数: { "salar ...
- element-ui上传组件,通过自定义请求上传文件
记录使用element-ui上传组件,通过自定义请求上传文件需要注意的地方. <el-upload ref="uploadMutiple" :auto-upload=&quo ...
- JAVA模拟HTTP post请求上传文件
在开发中,我们使用的比较多的HTTP请求方式基本上就是GET.POST.其中GET用于从服务器获取数据,POST主要用于向服务器提交一些表单数据,例如文件上传等.而我们在使用HTTP请求时中遇到的比较 ...
- springMVC+spring+hibernate注解上传文件到数据库,下载,多文件上传
数据库 CREATE TABLE `annex` ( `id` bigint() NOT NULL AUTO_INCREMENT, `realName` varchar() DEFAULT NULL, ...
- upload三种上传方式(上)---Servlet---post---commons-fileupload.1.2.1.jar方式请求上传文件
上传前进行的配置选项: 1.在下方的Servers中,右键你的tomcat--open,选中下面两个配置. 第一个:Serve modules without publishing 作用:tomcat ...
- 通过PHP CURL模拟请求上传文件|图片。
现在有一个需求就是在自己的服务器上传图片到其他服务器上面,过程:客户端上传图片->存放到本地服务器->再转发到第三方服务器; 由于前端Ajax受限制,只能通过服务器做转发了. 在PHP中通 ...
- 通过POST请求上传文件
转自:https://blog.csdn.net/zhangge3663/article/details/81218488 理论 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交 ...
随机推荐
- php 自带加密、解密函数
php 自带的加密函数 不可逆的加密函数为:md5().crypt() md5() 用来计算 MD5 哈稀.语法为:string md5(string str); crypt() 将字符串用 UNI ...
- DEDE会员注册邮件验证时,用户无法收到邮件的解决方法
本文以qq邮箱.163邮箱和易网库提供的企业邮箱为例,简要介绍在织梦(DEDECMS)中设置SMTP验证发送邮件的方法 一.在织梦中使用qq邮箱发送邮件 1.在织梦中使用qq邮箱发送邮件, 需要确保q ...
- POJ 1182——食物链——————【种类并查集】
食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status P ...
- 权限修饰符(public、protected、default、private)权限验证
一.测试方法 在authorityTest包中建类AuthorityValidate package authorityTest; public class AuthorityValidate { p ...
- EF Core 2.1 +数据库视图
1.参考文档 https://stackoverflow.com/questions/36012616/working-with-sql-views-in-entity-framework-core ...
- array_map()关于回调函数的总结
array_map()函数的第一个参数可以是匿名函数,系统函数,也可以是自己自定义的函!在全局空间下,这些函数的调用都很简单 在这里就不多说了!我们主要讨论的是该函数调用类中的方法和静态方法的区别 摘 ...
- 巧用CSS属性visibility与opacity代替鼠标经过的jQuery事件
一直使用jQuery的hover()函数来写鼠标经过事件,这几天发现其他同行直接使用CSS就搞定了.自己在线下也试了试: 当然也并不是没有前提条件,需要配合一些定位属性来使用(position/top ...
- *459. Repeated Substring Pattern (O(n^2)) two pointers could be better?
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- 屏蔽firefox浏览器连接失败页面的广告
现象 最近一直在使用firefox浏览器(版本:57.0.1(64位)),同步书签特别方便,但是最近发现当访问的一个不存在的网址时,连接失败页面竟然有广告!firefox不是号称没有广告吗? 分析 F ...
- IOS 拉伸图片(封装)
/** * 根据图片名返回一张能够自由拉伸的图片 */ +(UIImage *)resizedImage:(NSString *)name { UIImage *image=[UIImage imag ...