Springboot文件上传

本文介绍了一种基于Java的单个文件上传方法,通过HTML表单实现文件选择,并使用JavaScript进行文件类型的检查,确保只有PDF或图片格式可以上传。同时,展示了如何在Java类中定义上传文件的属性。

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

1.单个文件上传

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ include file="../../taglibs.jsp"%>

	<form:form action="/joint/disciplinary/save" modelAttribute="disciplinaryInfo" id="disciplinaryForm" method="post">
			<tr>
			
				<th>认定依据:</th>
				<td>
		           	<input type="file" style="display: none;" name="basisFile" id="basisFile" readonly="readonly" onchange="checkFile('basisFile','basis')"/>  
	 				<input name="basis" class="f-w200 u-ipt" readonly="readonly" /> 
	 				<input type="button" value="选择文件" onclick="basisFile.click();" class="u-btn u-btn-02 js-add" />
				</td>
			</tr>
			<tr>
				
				<th>报送函:</th>
				<td>
		           	<input type="file" style="display: none;" name="reportFile" id="reportFile" readonly="readonly" onchange="checkFile('reportFile','report')"/>  
	 				<input name="report" class="f-w200 u-ipt" readonly="readonly" /> 
	 				<input type="button" value="选择文件" onclick="reportFile.click();" class="u-btn u-btn-02 js-add" />
				</td>
			</tr>
		</table>
			
		<div class="btn-box">
			<input class="u-btn" id="form_save" type="submit" value="保存">
		    <input class="u-btn u-btn-03" type="reset" onclick="javascript:history.go(-1)" value="返回">
			
		</div>
	</form:form>
	
</div>
<script type="text/javascript" src="${ctx}/js/webuploader/webuploader.min.js"></script>
<script type="text/javascript" src="${ctx}/js/webuploader/image-upload/upload.js"></script>
<script type="text/javascript">
/**
 * 检查上传文件类型
 */
function checkFile(id,inputId) {
	var filePath = $("#"+id).val();
	if (filePath != null && filePath != '') {
		var f = filePath.split('.');
		if (f[f.length - 1] == 'pdf' || f[f.length - 1] == 'gif' || f[f.length - 1] == 'jpeg'  || f[f.length - 1] == 'png') {
			//展示文件路径
			$("#"+inputId).val(filePath);
		} else {
			//删除file标签中文件信息,不展示文件路径
			$("#"+id).val("");
			layer.alert("请选择pdf文件或图片");
			return;
		}
	} else {
		layer.alert("请选择文件");
		return;
	}
}

</script>
public class DisciplinaryInfo{

    /** @Fields reportFilePath: 报送函路径*/
	private String reportFilePath;
	
	/** @Fields reportFile:上传文件-报送函*/
	private MultipartFile reportFile;
	
	
	
	/** @Fields basisFile:上传文件-认定依据*/
	private MultipartFile basisFile;
	
    /** @Fields basisFileName: 认定依据名称*/
	private String basisFileName;
	
}

 

2.多种文件与其他信息一起保存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值