SpringMvc 上传/下载 文件

编程入门 行业动态 更新时间:2024-10-08 06:17:35

SpringMvc <a href=https://www.elefans.com/category/jswz/34/1771255.html style=上传/下载 文件"/>

SpringMvc 上传/下载 文件

1.配置 注解驱动/多部件解析器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:mvc=""xmlns:context = ""xmlns:is = ""is:schemaLocation ="://www.springframework/schema/beans/spring-beans-4.2.xsd://www.springframework/schema/mvc/spring-mvc-4.2.xsd ://www.springframework/schema/context/spring-context-4.2.xsd"
><!-- scan package beans --><context:component-scan base-package="com.springmvc.*"></context:component-scan><!-- 配置 springMvc 注解驱动 --><mvc:annotation-driven></mvc:annotation-driven><!-- 配置 多部件 --><bean class="org.springframework.web.multipartmons.CommonsMultipartResolver" id="multipartResolver"><!-- 可设置上传文件大小,上传类型,属性 等属性--></bean></beans>

2.编写 上传/下载 的 handler 方法

package com.springmvc.handler.fileupdownload;import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.URLEncoder;import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;@Controller
public class Fileupdownload {private static Logger rootLogger = Logger.getLogger(Fileupdownload.class);@RequestMapping(value="/fileUpLoad")public String fileUpLoad(@RequestParam(value="img")MultipartFile multipartFile,Model model){//原始文件名String fileName=multipartFile.getOriginalFilename();//文件大小String fileSize=String.valueOf(multipartFile.getSize());//文件mumi 类型String fileContentType=multipartFile.getContentType();rootLogger.debug("[fileName="+fileName+",size="+fileSize+",contentType="+fileContentType+"]");//文件存放目标位置File file = new File("f:/"+fileName);try {//文件转移到目标位置multipartFile.transferTo(file);} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();                                   } catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}model.addAttribute("fileName", fileName);model.addAttribute("fileSize", fileSize);model.addAttribute("fileContentType", fileContentType);return "forward:/fileupdownload/suc.jsp";}@RequestMapping(value="/fileDownLoad")public ResponseEntity<byte[]> fileDownLoad(){HttpHeaders heads=new HttpHeaders();try {String fileName = "f://11.txt";File file = new File(fileName);heads.setContentDispositionFormData("attachment",URLEncoder.encode(file.getName(), "UTF-8"));byte[] bytes= FileCopyUtils.copyToByteArray(file);ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(bytes,heads,HttpStatus.CREATED);return responseEntity;} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}
}

3.编写 submit.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form action ="<%=path %>/fileUpLoad.mvc" method="post" enctype="multipart/form-data"><input type = "file" name="img"></input><br/><br/><a href ="<%=path %>/fileDownLoad.mvc">下载</a><br/><br/><input type = "submit" ></input><br/><br/></form> </body>
</html>

4.编写文件上传的跳转页面 success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>成功上传一个文件<br />文件名 : ${fileName }<br />文件大小 :${fileSize }&nbsp;&nbsp;bytes<br />文件mumi类型 :${ fileContentType}<br /></body>
</html>

更多推荐

SpringMvc 上传/下载 文件

本文发布于:2024-03-14 05:02:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1735686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传   文件   SpringMvc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!