这种一般主要是文件上传时使用,最近因为这个浪费了一大堆时间,很是头痛那,相关的资料也不多,博主就按照自己的理解简单的介绍下吧。
如图所示
比如:
Content-Disposition: form-data; name=“name” 12ecc5a33e82173a3caf78ee4b5de8e4.jpg
那么name对应的值就是12ecc5a33e82173a3caf78ee4b5de8e4.jpg
接收文件的部分比较特殊,关于文件的一些参数可以通过接收到的文件对象进行获取。
struts2: (需要注意的是要extends=“struts-default”(主要是他的拦截器,当然可以是继承别的package),也要有上传所需要的jar包,commons-fileupload)
struts.xml配置:
<package name="upload" extends="struts-default">
<action name="upload" class="com.xxx.uploadfile.FileAction" method="upload"> </action>
</package>
public class FileAction extends ActionSupport {
//如有多个文件上传可以使用数组接收
private File file;
private String fileFileName;
private String fileContentType;
//(要接收的表单参数) ....
private String type;
public void upload() {
// to do something
}
//setter getter
public File getFile() {
return file;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFileFileName() {
return fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFile(File file) {
this.file = file;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
}
springmvc: (要有上传所需要的jar包,commons-fileupload)
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<property name="maxUploadSize" value="104857600" /> <!-- 设置允许上传的最大文件大小,以字节为单位计算。当设为-1时表示无限制,默认是-1 -->
<property name="maxInMemorySize" value="4096" /> <!-- 设置在文件上传时允许写到内存中的最大值,以字节为单位计算,默认是10240 -->
</bean>
//具体的接收参数方式按照业务需求设置
@RequestMapping("upload")
public void upload( @RequestParam("file") MultipartFile[] files,
@RequestParam(value = "type", required = false) String type,
@RequestParam(value = "id", required = false) Long id
}
这样就可以接收到对应的值了,如果还是没有需要进行排查,比如springmvc的可以查看web.xml定义的springmvc是否为org.springframework.web.servlet.DispatcherServlet,如果有其他拓展可能会被影响到了。