springMVC本地檔案上傳與下載
package com.minbao.controller;
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.util.Iterator; import java.util.UUID;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.minbao.common.utils.FilesMeta; import com.minbao.common.utils.ResultVo; import com.minbao.entity.THiddenDangerAmend; import com.minbao.service.THiddenDangerAmendService;
@Controller @RequestMapping(value="/upload") public class UploadController { @Autowired private THiddenDangerAmendService hiddenDangerAmendService; private final String fileRoot = "D:\\fileservice\\file\\"; /** * 上傳檔案到本地方法 */ @RequestMapping(value="/uploadFile") public @ResponseBody ResultVo uploadfile(MultipartHttpServletRequest request) { ResultVo rtVo = new ResultVo(); //檢查資料夾是否存在,檔案不存在則建立資料夾 String exhibtsImgDir = fileRoot; File folder = new File(exhibtsImgDir); if(!folder.exists()){ folder.mkdirs(); } //開始處理上傳檔案 Iterator<String> itr = request.getFileNames(); while (itr.hasNext()) { MultipartFile mpf = request.getFile(itr.next()); FilesMeta filemeta = new FilesMeta(); try { filemeta.setContent(mpf.getBytes()); filemeta.setFileSize(mpf.getSize()/1000); String fileExtension = mpf.getOriginalFilename().substring(mpf.getOriginalFilename().lastIndexOf(".")); String newName = UUID.randomUUID().toString()+fileExtension; filemeta.setFileName(mpf.getOriginalFilename()); filemeta.setUrl(newName); File newFile = new File(exhibtsImgDir+newName); mpf.transferTo(newFile); rtVo.setData(filemeta); } catch (IOException e) { rtVo.setMessage("檔案格式不正確或檔案錯誤"); rtVo.setIsOk(false); } } return rtVo; } /* * 本地檔案下載 */ @RequestMapping("/downloadFile") public void downloadFile(HttpServletResponse response,HttpServletRequest request,THiddenDangerAmend hiddenDangerAmend){ hiddenDangerAmend=hiddenDangerAmendService.selectById(hiddenDangerAmend.getId()); if(hiddenDangerAmend!=null) { String fileUrl=hiddenDangerAmend.getFileUrl(); String pathFile=fileRoot+fileUrl; try { // path是指欲下載的檔案的路徑。 File file = new File(pathFile); // 取得檔名。 String filename = file.getName(); // 取得檔案的字尾名。 String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); fileUrl=fileUrl+"."+ext; fileUrl = URLEncoder.encode(fileUrl,"UTF-8"); // 以流的形式下載檔案。 InputStream fis = new BufferedInputStream(new FileInputStream(pathFile)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 設定response的Header response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileUrl.getBytes())); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }