SSM框架 上传文件

首先需要两个jar包

然后在spring-mvc.xml文件中插入下面的代码

<!-- 定义文件上传解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设定默认编码 -->
        <property name="defaultEncoding" value="UTF-8" />
        <!-- 设定文件上传的最大值5MB,5*1024*1024 -->
        <property name="maxUploadSize" value="5242880" />
        <property name="maxInMemorySize" value="4096" />
    </bean>

下面是表单的代码,注意一定要加上enctype=“multipart/form-data”
method=“post”

<form action="${pageContext.request.contextPath }/uploadfile.do" method="post" enctype="multipart/form-data">
	<input type="file" name="pic" />
	<input type="submit" name="提交" />
</form>
@RequestMapping("/uploadfile.do")
	public String doUploadFile(@RequestParam MultipartFile pic){
		//设置一下保存的路径
		String path = "D:/pic/";
		File dir = new File(path);
        if (!dir.isDirectory())
            dir.mkdir();
        //给文件一个新的名字
        String filename = UUID.randomUUID().toString().replaceAll("-", "");
        //获取文件的扩展名
        String ext = FilenameUtils.getExtension(pic.getOriginalFilename());
        try {
        	//把文件存到指定的位置
			pic.transferTo(new File(path + filename + "." + ext));
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "redirect:/filetest.jsp";
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于SSM框架文件上传下载代码,可以分为两部分:前端页面和后端代码。 前端页面: 1. 文件上传页面(upload.jsp) ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>文件上传</title> </head> <body> <form method="post" action="fileUpload" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" value="上传"/> </form> </body> </html> ``` 2. 文件下载页面(download.jsp) ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>文件下载</title> </head> <body> <table> <thead> <tr> <th>文件名</th> <th>大小</th> <th>下载</th> </tr> </thead> <tbody> <c:forEach items="${files}" var="file"> <tr> <td>${file.name}</td> <td>${file.size}</td> <td><a href="fileDownload?fileName=${file.name}">下载</a></td> </tr> </c:forEach> </tbody> </table> </body> </html> ``` 后端代码: 1. 文件上传Controller(FileUploadController.java) ``` @Controller public class FileUploadController { @RequestMapping(value = "/fileUpload", method = RequestMethod.POST) @ResponseBody public String fileUpload(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return "请选择文件"; } String fileName = file.getOriginalFilename(); String filePath = "C:\\upload\\"; File dest = new File(filePath + fileName); try { file.transferTo(dest); return "文件上传成功"; } catch (IOException e) { return "文件上传失败"; } } } ``` 2. 文件下载Controller(FileDownloadController.java) ``` @Controller public class FileDownloadController { @RequestMapping(value = "/fileDownload") public void fileDownload(HttpServletRequest request, HttpServletResponse response, @RequestParam("fileName") String fileName) { String filePath = "C:\\upload\\"; File file = new File(filePath + fileName); if (file.exists()) { response.setContentType("application/octet-stream"); response.setContentLength((int) file.length()); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); try { FileInputStream in = new FileInputStream(file); OutputStream out = response.getOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } in.close(); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值