有時候,最美的驚喜并不是得到,而是學會付出。——《 安娜和她的云 》
最近放慢了寫作的速度,是因為我慢慢覺得有些東西還是沉淀一段時間后才會更有價值。
我們先來看看使用SPRing MVC實現文件上傳所需要的jar包
?12345678910 | < dependency > < groupId >commons-fileupload</ groupId > < artifactId >commons-fileupload</ artifactId > < version >1.3.1</ version > </ dependency > < dependency > < groupId >commons-io</ groupId > < artifactId >commons-io</ artifactId > < version >2.4</ version > </ dependency > |
這里只列出了上傳下載所需要的jar包,使用Spring MVC框架的jar包我在上一篇文章已經寫過了,如果還沒看的請點擊鏈接springmvc創建web項目基礎之二-spring-mvc與mybatis整合詳解先去看我的上一篇文章。
一、前端頁面實現
前端頁面很簡單,樣子如下:
<div class="col-lg-12" style="margin-top: 10px"> <form id="upLoad_form" method="post" enctype="multipart/form-data"> <input type="file" id="picture" name="file"> <button type="button" id="upload_btn" class="btn btn-primary" >上傳圖片</button> </form> </div>?二、后端實現
1.控制器BookShopController
在這個項目里簡單實現一個上傳圖片的功能。
?
@Resource private BookShopService mService;/** * 上傳圖片 * * @param MultipartFile file 頁面選中的文件 * @param HttpServletRequest request 請求 * @param HttpServletResponse response 響應 * */ @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public void upload(@RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse response) { try { mService.uploadFile(file,request,response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } |
2.服務接口BookShopService
public interface BookShopService {public void uploadFile(MultipartFile file,HttpServletRequest request, HttpServletResponse response) throws Exception;}?3.服務接口的實現類BookShopServiceImpl
@Servicepublic class BookShopServiceImpl implements BookShopService {@Override public void uploadFile(MultipartFile file,HttpServletRequest request, HttpServletResponse response) throws Exception {BookShopUploadUtil.fileUp(file,request,response); }}?4.上傳文件工具類BookShopUploadUtil
這個是實現上傳文件的核心實現
public class BookShopUploadUtil { /** * 上傳圖片 */ public static void fileUp(MultipartFile file,HttpServletRequest request, HttpServletResponse response) throws Exception { String path="";//保存圖片路徑 String filePath = Consts.CONTEXTPATH;//顯示圖片路徑(相對路徑) try { if (!file.isEmpty()){ //得到服務器中保存文件的絕對路徑 path = request.getServletContext().getRealPath(Consts.SAVE_PATH + "/"); //新建文件夾 newFolder(path); //新建文件夾完成后將路徑添加文件名 path += file.getOriginalFilename(); //頁面顯示圖片的相對路徑 filePath += "/" + Consts.SAVE_PATH + "/" + file.getOriginalFilename(); //使用StreamsAPI方式拷貝文件 Streams.copy(file.getInputStream(),new FileOutputStream(path),true); //將上面得到的圖片相對路徑返回給頁面 JSONObject json = new JSONObject(); json.accumulate(Consts.SRC, filePath); json.accumulate("error", false); json.accumulate("message", "上傳成功!"); PrintWriter out = response.getWriter(); out.print(json.toString()); out.flush(); out.close(); } } catch (Exception e) { System.out.println("文件上傳失敗"); e.printStackTrace(); path=""; } } /** * 創建文件夾 * * @param folderPath */ public static void newFolder(String folderPath) { try { File myFilePath = new File(folderPath); if (!myFilePath.exists()) { //創建多級文件夾 myFilePath.mkdirs(); System.out.println("創建文件夾路徑:" + folderPath); } } catch (Exception e) { System.out.println("新建文件夾操作出錯"); e.printStackTrace(); } }}?Consts.SAVE_PATH是一個字符串常量,表示存放路徑
public class Consts {public final static String SAVE_PATH = "resources/bookshopupload/savefile/"; }?至此實現就完成了,效果如下
如果你喜歡我的文章請掃描主頁的微信公眾號二維碼,每天都有新推文。
如果你喜歡我的文章請收藏我的個人網站:http://www.bubblyyi.com
新聞熱點
疑難解答