欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

spring-配置文件

时间:2023-06-17
pom配置

<?xml version="1.0" encoding="UTF-8"?> 4.0.0 org.example ssmbuild 1.0-SNAPSHOT 8 8 junit junit 4.13 test mysql mysql-connector-java 8.0.27 com.mchange c3p0 0.9.5.5 javax.servlet servlet-api 2.5 javax.servlet.jsp jsp-api 2.2 javax.servlet jstl 1.2 org.mybatis mybatis 3.5.2 org.mybatis mybatis-spring 2.0.6 org.springframework spring-webmvc 5.3.12 org.springframework spring-jdbc 5.1.9.RELEASE org.projectlombok lombok 1.16.0 org.aspectj aspectjweaver 1.9.4 com.fasterxml.jackson.core jackson-core 2.12.5 com.fasterxml.jackson.core jackson-databind 2.12.5 org.codehaus.jackson jackson-mapper-asl 1.9.13 commons-fileupload commons-fileupload 1.3.3 javax.servlet javax.servlet-api 4.0.1 src/main/java ***.xml false src/main/resources ***.xml false

web.xml

配置springmvc

<?xml version="1.0" encoding="UTF-8"?> springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext.xml 1 springmvc / encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding utf-8 encodingFilter //第一种方法@RestControllerpublic class FileController { @RequestMapping("/upload") public String fileupload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException { //获取文件名,file.getOriginalFilename(); String uploadFileName = file.getOriginalFilename(); //如果文件名为空,直接回到首页 if ("".equals(uploadFileName)) { return "redirect:/index.jsp"; } System.out.println("上传文件名:" + uploadFileName); //上传路径保存设置,javax-servlet包版本要在3.0以上 String path = request.getServletContext().getRealPath("/upload"); //如果路径不存在,创建一个 File realPath = new File(path); if (!realPath.exists()) { realPath.mkdir(); } System.out.println("上传文件保存地址" + realPath); InputStream is=file.getInputStream();//文件输入流 OutputStream os=new FileOutputStream(new File(realPath,uploadFileName));//文件输出流 //读取写出 int len=0; byte[] buffer=new byte[1024]; while((len=is.read(buffer))!=-1){ os.write(buffer,0,len); os.flush(); } os.close(); is.close(); return "redirect:index.jsp"; } //第二种方法 @RequestMapping("/upload2") public String fileUpload2(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request) throws IOException { String path=request.getServletContext().getRealPath("/upload"); File realPath=new File(path); if(!realPath.exists()){ realPath.mkdir(); } System.out.println("上传文件保存地址"+realPath); file.transferTo(new File(realPath+"/" + file.getOriginalFilename())); return "redirect:/index.jsp"; }}//下载 @RequestMapping("/download") public String downloads(HttpServletResponse response,HttpServletRequest request) throws IOException { String path=request.getServletContext().getRealPath("/upload"); String fileName="基础语法.jpg"; //1.设置response响应头 response.reset();//设置页面不缓存,清空buffer response.setCharacterEncoding("UTF-8");//字符编码 response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(fileName,"UTF-8")); File file=new File(path,fileName); //2.读取文件--输入流 InputStream input=new FileInputStream(file); OutputStream out=new response.getOutputStream(); byte[] b=new byte[1024]; int index=0; while((index=input.read(b))!=-1){ out.write(b,0,index); out.flush(); }// out=null;// response.flushBuffer(); out.close(); input.close(); return null; }

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。