Spring上传File文件

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/sinat_28505133/article/details/80604622

配置文件: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



猜你喜欢

转载自blog.csdn.net/sinat_28505133/article/details/80604622