目录
2.打开代理ReverseProxy_windows_amd64.exe(顺序不能错)
前言:
小编详细的向读者展示了:如在新建一个Maven项目的情况下去搭建一个Springmvc ,mybatis,maven集成然后实现SpringMvc的CRUD,以及对于效果图的展示!本次小编带来的的关于文件的上传以及下载!
- <dependency>
- <groupId>commons-fileuploadgroupId>
- <artifactId>commons-fileuploadartifactId>
- <version>1.3.3version>
- dependency>
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
-
- <property name="defaultEncoding" value="UTF-8">property>
-
- <property name="maxUploadSize" value="52428800">property>
-
- <property name="resolveLazily" value="true"/>
- bean>

- <%--
- Created by IntelliJ IDEA.
- User: lzzxq
- Date: 2023/9/9
- Time: 14:26
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
-
书籍头像上传 -
- "text" name="bid" readonly="readonly" value="${param.bid}"/>
- "file" name="xxx"/>
- "submit" value="上传图片"/>
-
后端利用muiltpartFile类,接收前端传递到后端的文件
- @RequestMapping("/upload")
- //头像上传
- public String upload(Book book,MultipartFile xxx){
- try {
- //上传的图片存放地址
- String dir=PropertiesUtil.getValue("dir");
- // 网络访问地址
- String server=PropertiesUtil.getValue("server");
- String filename = xxx.getOriginalFilename();
- System.out.println("文件名:"+filename);
- System.out.println("文件类型:"+xxx.getContentType());
- FileUtils.copyInputStreamToFile(xxx.getInputStream(),new File(dir+filename));
- //修改字段名的属性
- book.setBname(server+filename);
- bookBiz.updateByPrimaryKeySelective(book);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "redirect:list";
- }



- //文件下载
- @RequestMapping(value="/download")
- public ResponseEntity<byte[]> download(Book book, HttpServletRequest req){
- try {
- //先根据文件id查询对应图片信息
- Book bk=this.bookBiz.selectByPrimaryKey(book.getBid());
- String diskPath = PropertiesUtil.getValue("dir");
- String reqPath = PropertiesUtil.getValue("server");
- String realPath = bk.getBname().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;
- }
"${pageContext.request.contextPath }/book/download?bid=${b.bid}">图片下载



第一行输入: http://127.0.0.1:8888/GUID
其次在浏览器输入
在将GUID赋值GUID到网址前面

- //多文件上传
- @RequestMapping("/uploads")
- public String uploads(HttpServletRequest req, Book book, 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";
- }
- "file" name="files" multiple>
-
-
