🥳🥳Welcome Huihui's Code World ! !🥳🥳
接下来看看由辉辉所写的关于SpringMVC的相关操作吧
需要添加的依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
在WEB-INF目录下的spring-mvc.xml文件中添加文件上传解析器
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 必须和用户JSP 的pageEncoding属性一致,以便正确解析表单的内容 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 文件最大大小(字节) 1024*1024*50=50M-->
<property name="maxUploadSize" value="52428800"></property>
<!--resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常-->
<property name="resolveLazily" value="true"/>
</bean>
工具类
package com.wh.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil {
public static String getValue(String key) throws IOException {
Properties p = new Properties();
InputStream in = PropertiesUtil.class.getResourceAsStream("/resource.properties");
p.load(in);
return p.getProperty(key);
}
}
resource.properties
dir=E:/temp/
server=/upload/
一.文件上传
1.效果演示
2.代码
controller
//页面跳转 @RequestMapping("/page") public String page(Clazz clazz,HttpServletRequest req){ if(clazz!=null && clazz.getCid()!=null){ Clazz clazzs = clazzBiz.selectByPrimaryKey(clazz.getCid()); req.setAttribute("param",clazzs); } return "clz/upload"; } @RequestMapping("/uploads") public String upload(HttpServletRequest req, Clazz clazz, MultipartFile cfile){ try { //思路: //1) 将上传图片保存到服务器中的指定位置 String dir = PropertiesUtil.getValue("dir"); String server = PropertiesUtil.getValue("server"); String filename = cfile.getOriginalFilename(); FileUtils.copyInputStreamToFile(cfile.getInputStream(),new File(dir+filename)); //2) 更新数据库表t_struts_class图片记录 clazz.setPic(server+ filename); clazzBiz.updateByPrimaryKeySelective(clazz); } catch (Exception e) { e.printStackTrace(); } return "redirect:list"; }
jsp
<%-- Created by IntelliJ IDEA. User: W Date: 2023/9/9 Time: 15:35 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath }/smvc4/uploads" method="post" enctype="multipart/form-data"> <label>班级编号:</label><input type="text" name="cid" readonly="readonly" value="${param.cid}"/><br/> <label>班级图片:</label><input type="file" name="cfile"/><br/> <input type="submit" value="上传图片"/> </form> <%--多文件--%> </body> </html>
二.文件下载
1.效果演示
2.代码
controller
@RequestMapping(value="/download") public ResponseEntity<byte[]> download(Clazz clazz, HttpServletRequest req){ try { //先根据文件id查询对应图片信息 Clazz clz = this.clazzBiz.selectByPrimaryKey(clazz.getCid()); String diskPath = PropertiesUtil.getValue("dir"); String reqPath = PropertiesUtil.getValue("server"); String realPath = clz.getPic().replace(reqPath,diskPath); String fileName = realPath.substring(realPath.lastIndexOf("/")+1); //下载关键代码 File file=new File(realPath); HttpHeaders headers = new HttpHeaders();//http头信息 String downloadFileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");//设置编码 headers.setContentDispositionFormData("attachment", downloadFileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); //MediaType:互联网媒介类型 contentType:具体请求中的媒体类型信息 return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK); }catch (Exception e){ e.printStackTrace(); } return null; }
三.多文件上传
1.效果演示
2.代码
controller
@RequestMapping("/myuploads") public String uploads(HttpServletRequest req, Clazz clazz, MultipartFile[] files){ try { StringBuffer sb = new StringBuffer(); for (MultipartFile cfile : files) { //思路: //1) 将上传图片保存到服务器中的指定位置 String dir = PropertiesUtil.getValue("dir"); String server = PropertiesUtil.getValue("server"); String filename = cfile.getOriginalFilename(); FileUtils.copyInputStreamToFile(cfile.getInputStream(),new File(dir+filename)); sb.append(filename).append(","); } System.out.println(sb.toString()); } catch (Exception e) { e.printStackTrace(); } return "redirect:list"; }
jsp
<%-- Created by IntelliJ IDEA. User: W Date: 2023/9/9 Time: 15:35 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--多文件--%> <form method="post" action="${pageContext.request.contextPath}/smvc4/myuploads" enctype="multipart/form-data"> <input type="file" name="files" multiple> <button type="submit">上传</button> </form> </body> </html>
好啦,今天的分享就到这了,希望能够帮到你呢!😊😊