Spring上传File文件

配置文件:spring-mvc.xml

<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<property name="defaultEncoding" value="utf-8" />
</bean>

jsp代码:前端框架H-ui

<div class="row cl">
	<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>安装包路径:</label>
	<div class="formControls col-xs-8 col-sm-9">
	    <span class="btn-upload form-group">
	        <input class="input-text upload-url" type="text" readonly name="path" id="path">
		<span style="padding-left:10px"><a href="javascript:void();" class="btn btn-primary radius"> 浏览文件</a></span>
	        <input type="file" name="file" id="file" class="input-file">
	    </span>
	</div>
</div>

后台:

使用【MultipartFile file】接收文件参数,

// 上传文件路径
String path = this.fileService.getAbsPath() + "/resources/upload/app/";
File imgDir = new File(path);
if (!imgDir.exists())
{
    imgDir.mkdirs(); // 创建存储目录
}
if (file != null && !file.isEmpty())
{
    // 上传文件名
    String filename = file.getOriginalFilename();
    // 将上传文件保存到一个目标文件当中
    String filePaths = path + UUID.randomUUID().toString().replace("-", "") + "."
                + filename.substring(filename.lastIndexOf('.') + 1);
    file.transferTo(new File(filePaths));
}
此处提供一篇优质的博客: https://blog.csdn.net/qian_ch/article/details/69258465



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个猴子请来的救兵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值