struts2,springmvc (multipart/form-data)RequestPayload数据获取的方式

本文介绍如何在Struts2与SpringMVC框架中实现文件上传功能,包括配置示例及所需依赖库,并提供了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这种一般主要是文件上传时使用,最近因为这个浪费了一大堆时间,很是头痛那,相关的资料也不多,博主就按照自己的理解简单的介绍下吧。

这里写图片描述

如图所示

比如:
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,如果有其他拓展可能会被影响到了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值