資源簡介
SpringMVC文件上傳與下載的實現.rar
代碼片段和文件信息
package?com.mlxp.controller;
import?java.io.File;
import?java.io.OutputStream;
import?java.net.URLDecoder;
import?javax.servlet.http.HttpServletRequest;
import?javax.servlet.http.HttpServletResponse;
import?org.apache.commons.io.FileUtils;
import?org.springframework.http.HttpHeaders;
import?org.springframework.http.HttpStatus;
import?org.springframework.http.MediaType;
import?org.springframework.http.ResponseEntity;
import?org.springframework.stereotype.Controller;
import?org.springframework.ui.Model;
import?org.springframework.web.bind.annotation.ModelAttribute;
import?org.springframework.web.bind.annotation.RequestMapping;
import?org.springframework.web.bind.annotation.RequestMethod;
import?org.springframework.web.bind.annotation.RequestParam;
import?org.springframework.web.multipart.MultipartFile;
import?com.mlxp.entity.User;
@Controller
@RequestMapping(“/hellorabbit/*.do“)
public?class?DownloadController?{
@RequestMapping
public?String?hellomlxp(){
System.out.println(“hellomlxp“);
return?“upload“;
}
@RequestMapping
public?String?hellouser(){
System.out.println(“hellouser“);
return?“register“;
}
@RequestMapping(method=RequestMethod.POST)
????public?String?upload(HttpServletRequest?request
???????????@RequestParam(“description“)?String?description
???????????@RequestParam(“file“)?MultipartFile?fileModel?model)?throws?Exception?{
????????System.out.println(description);
//??????//不使用注解
// MultipartHttpServletRequest?multipartRequest?=?(MultipartHttpServletRequest)?request;
// //獲取file
// MultipartFile?file?=?multipartRequest.getFile(“file“);
????????//如果文件不為空,寫入上傳路徑
????????if(!file.isEmpty())?{
//????????????//上傳文件路徑
//????????????String?path?=?request.getSession().getServletContext().getRealPath(“/ooxx/“);
???????? //由配置文件獲取
???????? String?path=(String)?request.getSession().getServletContext().getAttribute(“path“);
????????????//上傳文件名
????????????String?filename?=?file.getOriginalFilename();
????????????File?filepath?=?new?File(pathfilename);
????????????//判斷路徑是否存在,如果不存在就創建一個
????????????if?(!filepath.getParentFile().exists())?{?
????????????????filepath.getParentFile().mkdirs();
????????????}?
//????????????//?讀取要下載的文件,保存到文件輸入流
//????????????InputStream?in?=?file.getInputStream();
//????????????//?創建輸出流
//????????????BufferedOutputStream?out?=?new?BufferedOutputStream(new?FileOutputStream(filepath));
//????????????//?創建緩沖區
//????????????byte[]?buffer?=?new?byte[32?*?10?*?1024];
//???? int?bytesRead?=?0;
//???? //?循環將輸入流中的內容讀取到緩沖區當中
//???? while?((bytesRead?=?in.read(buffer))?!=?-1){
//???? //?輸出緩沖區的內容到瀏覽器,實現文件下載
//???? ????out.write(buffer?0?bytesRead);
//???? }
//???? //?關閉輸出流
//????????????out.close();
//????????????//?關閉文件輸入流
// in.close();
????????????//將上傳文件保存到一個目標文件當中
????????????file.transferTo(new?File(path?+?File.separator?+?filename));
????????????model.addAttribute(“filename“?filename);
????????????return?“download
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????852??2017-09-26?13:30??Download_demo\.classpath
?????文件???????1046??2017-09-21?13:35??Download_demo\.project
?????文件????????517??2017-09-26?13:30??Download_demo\.settings\.jsdtscope
?????文件????????364??2017-09-15?13:59??Download_demo\.settings\org.eclipse.jdt.core.prefs
?????文件????????117??2017-09-21?13:36??Download_demo\.settings\org.eclipse.jst.j2ee.ejb.annotations.xdoclet.prefs
?????文件????????463??2017-09-26?13:30??Download_demo\.settings\org.eclipse.wst.common.component
?????文件????????345??2017-09-26?13:30??Download_demo\.settings\org.eclipse.wst.common.project.facet.core.xm
?????文件?????????49??2017-09-15?13:59??Download_demo\.settings\org.eclipse.wst.jsdt.ui.superType.container
?????文件??????????6??2017-09-15?13:59??Download_demo\.settings\org.eclipse.wst.jsdt.ui.superType.name
?????文件???????6319??2017-09-26?13:39??Download_demo\build\classes\com\mlxp\controller\DownloadController.class
?????文件????????909??2017-09-26?13:39??Download_demo\build\classes\com\mlxp\entity\User.class
?????文件???????2296??2017-09-26?13:39??Download_demo\build\classes\com\mlxp\listener\PropertiesListener.class
?????文件?????????13??2017-09-26?11:16??Download_demo\build\classes\path.properties
?????文件???????2105??2017-09-26?13:30??Download_demo\build\classes\spring-mvc.xm
?????文件????????843??2017-09-26?13:30??Download_demo\build\classes\spring.xm
?????文件???????6913??2017-09-26?13:39??Download_demo\src\com\mlxp\controller\DownloadController.java
?????文件????????635??2017-09-21?15:11??Download_demo\src\com\mlxp\entity\User.java
?????文件???????1769??2017-09-26?13:30??Download_demo\src\com\mlxp\listener\PropertiesListener.java
?????文件?????????13??2017-09-26?11:16??Download_demo\src\path.properties
?????文件???????2105??2017-09-26?13:30??Download_demo\src\spring-mvc.xm
?????文件????????843??2017-09-26?13:30??Download_demo\src\spring.xm
?????文件?????????86??2017-09-26?13:30??Download_demo\WebContent\index.jsp
?????文件??????78601??2015-01-02?15:00??Download_demo\WebContent\js\jquery-1.4.4.min.js
?????文件?????????39??2017-09-15?13:59??Download_demo\WebContent\me
?????文件?????188671??2015-01-02?15:59??Download_demo\WebContent\WEB-INF\lib\commons-beanutils-1.7.0.jar
?????文件??????46725??2015-01-02?15:59??Download_demo\WebContent\WEB-INF\lib\commons-codec-1.3.jar
?????文件?????571259??2015-01-02?15:59??Download_demo\WebContent\WEB-INF\lib\commons-collections-3.2.jar
?????文件??????71442??2015-01-02?15:59??Download_demo\WebContent\WEB-INF\lib\commons-discovery-0.2.jar
?????文件??????59590??2015-01-02?15:59??Download_demo\WebContent\WEB-INF\lib\commons-fileupload-1.2.2.jar
?????文件?????278799??2015-01-02?15:59??Download_demo\WebContent\WEB-INF\lib\commons-httpclient-3.0-rc4.jar
............此處省略55個文件信息
評論
共有 條評論