spring-文件上传
springmvc實現簡單文件上傳和下載
思路:1:maven導入common-fileupload包?
2:springmvc.xml導入文件多分布解析器
3:使用MultipartFile獲取上傳的文件名和轉換此文件為服務器上面的指定目錄下文件(上傳的jsp必須為multipart/form-data,不然multipartfile獲取的值為null)
4:下載: 使用FileCopyUtils.copyToByteArray(file)將目標文件轉換為二進制數組,設置響應頭和下載的文件名稱,構建ResponseEntity對象返回到瀏覽器
1
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
2
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1048576"></property>
</bean>
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="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">文件 <input type="file" name="aa"/> <a href="${pageContext.request.contextPath}/download?fileName=Servlet.pdf">下載</a> <input type="submit"/></form></body> </html>servlet代碼
package com.crazy.goods.tools.fileupload;import java.io.File; import java.io.IOException; import java.net.URLEncoder;import javax.servlet.http.HttpServletResponse;import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile;/*** @author Administrator* 創建時間:2017年7月6日上午8:46:53*/ @Controller public class UploadServlet {public String filePath="c:";@RequestMapping(value="/upload")public String upload(MultipartFile aa,HttpServletResponse response) throws IllegalStateException, IOException {String originalFilename = aa.getOriginalFilename();//獲取到的是文件的名字String name = aa.getName(); //或者的是aa的值String destFile = filePath+"/"+originalFilename;aa.transferTo(new File(destFile)); //將文件轉換為路徑下面的文件response.getWriter().println("upload sucess");return null;}@RequestMapping(value="/upload1")public String uploadpage() {return "upload";}@RequestMapping(value="download")public ResponseEntity<byte[]> down(String fileName) throws IOException{//需要下載的目標文件File file=new File(filePath+"/"+fileName);//讀取目標文件為二進制數組byte[] fileByte=FileCopyUtils.copyToByteArray(file);//設置響應頭HttpHeaders hh=new HttpHeaders();//設置下載的文件的名稱hh.setContentDispositionFormData("attachment", URLEncoder.encode(fileName, "UTF-8"));//構建ResponseEntity對象ResponseEntity<byte[]> re=new ResponseEntity<byte[]>(fileByte, hh, HttpStatus.CREATED);return re;} }?
轉載于:https://www.cnblogs.com/wdx330616/p/7128213.html
總結
以上是生活随笔為你收集整理的spring-文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一群人花费了四年的时间以NASA的数据巨
- 下一篇: BZOJ 2748: [HAOI2012