spring html上传文件,SpringMVC文件上传的方法介绍(代码)

本篇文章给大家带来的内容是关于SpringMVC文件上传的方法介绍(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

SpringMVC为文件上传提供了直接的支持,这种支持是用即插即用的MultipartResolver实现的。SpringMVC使用Apache Commons FileUpload技术实现了一个MultipartResolver实现类:CommonsMultipartResolver。因此,SpringMVC的文件上传还需要依赖Apache Commons FileUpload的组件。

1. 添加pom依赖

commons-io

commons-io

2.3

commons-fileupload

commons-fileupload

1.3.2

2. 配置文件上传bean

在spring mvc配置文件中增加一个文件上传bean。

3.文件上传

文件上传是项目开发中最常见的功能。为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data。只有在这样的情况下,浏览器才会把用户选择的文件以二进制数据发送给服务器。

上传文件界面:upload_form.jsp

文件上传

文件1:

文件2:

文件3:

用户名:

密码:

上传结果返回界面:upload_result.jsp

pageEncoding="UTF-8"%>

Insert title here

上传结果为:${message}

注意:要提前创建好存储文件的文件夹,比如我的路径为:"D:staticResourcesTestimgupload"。

FileController.java@Controller

@RequestMapping("/SpringMVCDemo1")

public class FileController {

/**

* 跳转到上传页面

* @GetMapping 是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。

*/

@GetMapping("/gotoUploadForm")

public String index() {

return "/upload_form.jsp";

}

/**

* 上传单个文件

* 通过MultipartFile读取文件信息,如果文件为空跳转到结果页并给出提示;

* 如果不为空读取文件流并写入到指定目录,最后将结果展示到页面

* @param multipartFile

* @PostMapping 是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。

*/

@PostMapping("/upload")

public String uploadSingleFile(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request){

if (multipartFile.isEmpty()){

request.setAttribute("message", "Please select a file to upload '");

return "/upload_result.jsp";

}

try {

String contentType = multipartFile.getContentType();

String originalFilename = multipartFile.getOriginalFilename();

byte[] bytes = multipartFile.getBytes();

System.out.println("上传文件名为-->" + originalFilename);

System.out.println("上传文件类型为-->" + contentType);

System.out.println("上传文件大小为-->"+bytes.length);

//filePath为存储路径

String filePath = "d:/staticResourcesTest";

System.out.println("filePath-->" + filePath);

//存储在staticResourcesTest下的imgupload文件夹下

File parentPath = new File(filePath, "imgupload");

System.out.println("上传目的地为-->"+parentPath.getAbsolutePath());

try {

File destFile = new File(parentPath,originalFilename);//上传目的地

FileUtils.writeByteArrayToFile(destFile,multipartFile.getBytes());

} catch (Exception e) {

e.printStackTrace();

}

request.setAttribute("message", "You successfully uploaded '" + multipartFile.getOriginalFilename() + "'");

} catch (IOException e) {

e.printStackTrace();

}

return "/upload_result.jsp";

}

/**

* 上传多个文件,同时接受业务数据

* @param origFiles

* @param request

* @param user

* @return

*/

@PostMapping("/uploadMultiFiles")

public String uploadMultiFiles(@RequestParam("file") List origFiles, HttpServletRequest request, User user) {

//User为实体类

System.out.println("User=="+user);

if (origFiles.isEmpty()) {

request.setAttribute("message", "Please select a file to upload '");

return "/upload_result.jsp";

}

try {

for (MultipartFile origFile : origFiles) {

String contentType = origFile.getContentType();

String fileName = origFile.getOriginalFilename();

byte[] bytes = origFile.getBytes();

System.out.println("上传文件名为-->" + fileName);

System.out.println("上传文件类型为-->" + contentType);

System.out.println("上传文件大小为-->"+bytes.length);

String filePath = "d:/staticResourcesTest";

System.out.println("上传目的地为-->"+filePath);

try {

//上传目的地(staticResourcesTest文件夹下)

File destFile = new File(filePath,fileName);

FileUtils.writeByteArrayToFile(destFile,origFile.getBytes());

} catch (Exception e) {

e.printStackTrace();

}

}

request.setAttribute("message", "You successfully uploaded '");

} catch (IOException e) {

e.printStackTrace();

}

return "/upload_result.jsp";

}

}

本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的Java教程视频栏目!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值