因爲第一次使用 spring boot 在開發項目, 在文件上傳下載這塊 出現了兩個BUG: java
BUG1: 在服務器Jar包形式下,文件(圖片)上傳成功後, 瀏覽器訪問不到;web
BUG2: 文件下載時, 系統報錯: ajax
org.apache.catalina.connector.ClientAbortException: java.io.IOException: 您的主機中的軟件終止了一個已創建的鏈接....
BUG1: https://www.oschina.net/question/3681868_2281702spring
BUG2: 在百度和試驗一波後,發現問題應該是ajax請求的問題, 只須要將下載接口開放爲GET請求經過 鏈接來下載便可;apache
1, 文件上傳工具類: 瀏覽器
package com.gy.fast.common.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.util.Arrays; import java.util.Date; import java.util.HashSet; import java.util.Set; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.springframework.util.ResourceUtils; import org.springframework.web.multipart.MultipartFile; import com.gy.fast.common.exception.SysException; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.RandomUtil; /** * 文件上傳工具 * * @author geYang * @date 2018-06-13 */ public class UploadUtils { /** * 文件指定訪問URL */ public static final String BASE_URL = "/files/"; /** * 文件指定存放目錄 */ public static final String BASE_PATH = "uploadfiles/"; /** * 用戶圖片存放目錄 */ public static final String USER_IMAGE = "userimage/"; /** * 其餘文件存放目錄 */ public static final String OTHERF = "outher/"; /** * 容許上傳文件集合 */ protected static String[] ALLOW_FORMAT = { ".jpg", ".png", ".gif", ".jpeg", ".txt", ".zip", ".doc", ".docx", ".xls", ".xlsx" }; /** * 獲取項目絕對路徑 * @return String( E:\FrindHR\niuniu_lawyer_internal\niuer-law\target\classes ) * @author geYang * @date 2018-06-13 06:47 * */ public static String getClassFilePath() { try { String classPath = ResourceUtils.getURL("classpath:").getPath(); File classFile = new File(classPath); if (!classFile.exists()) { classFile = new File(""); } String classFilePath = classFile.getAbsolutePath(); System.out.println("classFilePath(項目根路徑): " + classFilePath); return classFilePath; } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } /** * 文件存放的絕對路徑 * @param path 默認爲 OTHER(other) * @author geYang * @date 2018-06-13 07:06 * */ public static String getUploadFilePath(String path) { String classFilePath = getClassFilePath(); path = CommonUtil.isBlank(path) || path.equals(BASE_URL) || path.equals(BASE_PATH) ? OTHERF : path; File uploadFile = new File(classFilePath, BASE_PATH + path); if (!uploadFile.exists()) { uploadFile.mkdirs(); } String uploadFilePath = uploadFile.getAbsolutePath(); System.out.println("uploadFilePath(文件上傳路徑): " + uploadFilePath); return uploadFilePath; } /** * 文件上傳 * @param file 文件 * @param path 存放目錄 * @author geYang * @date 2018-06-13 07:09 * */ public static String upload(MultipartFile file, String path) { if (file.isEmpty()) { return null; } String oldfileName = file.getOriginalFilename(); String fileName = getfileName(oldfileName); String fileUrl = null; try { // 文件存放目錄 String uploadFilePath = getUploadFilePath(path); uploadFile(file.getBytes(), uploadFilePath, fileName); fileUrl = getFileUrl(path, fileName); } catch (IOException e) { e.printStackTrace(); } return fileUrl; } /** * 獲取文件訪問路徑 * */ public static String getFileUrl(String path, String fileName) { String fileUrl = BASE_URL + path + fileName; System.out.println("fileUrl(文件訪問路徑): " + fileUrl); return fileUrl; } /** * 獲取文件存放路徑(相對路徑) * @param fileUrl 文件訪問路徑 * @return uploadfiles/outher/08.jpg * @author geYang * @date 2018-06-13 07:25 * */ private static String getUploadPath(String fileUrl) { String uploadPath = fileUrl.replace(BASE_URL, BASE_PATH); System.out.println("uploadPath(文件存放路徑): " + uploadPath); return uploadPath; } /** * 獲取文件存放全路徑(絕對) * @param fileUrl * @return E:\FrindHR\niuniu_lawyer_internal\niuer-law\target\test-classes/uploadfiles/outher/08.jpg * @author geYang * @date 2018-06-13 07:24 * */ public static String getUploadPathAll(String fileUrl) { String uploadPathAll = getClassFilePath() + "/" + getUploadPath(fileUrl); System.out.println("uploadPathAll(文件存放絕對路徑): " + uploadPathAll); return uploadPathAll; } /** * 生成隨機文件名 * * @param originalName * @return * @author geYang * @date 2018-06-13 10:54 */ private static String getfileName(String originalName) { String suffix = getSuffix(originalName); StringBuffer name = new StringBuffer(); name.append(DateUtil.format(new Date(), "yyyyMMddHHmmss")).append(RandomUtil.randomString(5)).append(suffix); return name.toString(); } /** * 獲取文件後綴名 */ public static String getSuffix(String name) { String suffix = name.substring(name.lastIndexOf(".")); if (!checkSuffix(suffix)) { throw new SysException("文件不合法"); } return suffix; } /** * 判斷文件名是否合格 * @param name * @return * @author geYang * @date 2018-06-13 08:35 * */ public static boolean checkName(String name) { return checkSuffix(getSuffix(name)); } /** * 校驗後綴名是否合法 * */ private static boolean checkSuffix(String suffix) { Set<String> setSuffix = new HashSet<>(Arrays.asList(ALLOW_FORMAT)); return setSuffix.contains(suffix); } /** * 文件上傳 * */ public static void uploadFile(byte[] file, String filePath, String fileName) throws IOException { File targetFile = new File(filePath); if (!targetFile.exists()) { targetFile.mkdirs(); } FileOutputStream out = new FileOutputStream(filePath + "/" + fileName); out.write(file); out.flush(); out.close(); } /** * 獲取文件下載數據 * @param url * @return * @author geYang * @date 2018-06-13 10:20 * */ private static byte[] getDownloadByte(String fileUrl) { try { String uploadPath = getUploadPathAll(fileUrl); InputStream inputStream = new FileInputStream(new File(uploadPath)); byte[] data = new byte[inputStream.available()]; inputStream.read(data); inputStream.close(); return data; } catch (IOException e) { e.printStackTrace(); throw new SysException("文件下載異常"); } } /** * 文件下載 * @param fileUrl 文件訪問路徑 * @param fileName 定義下載文件名,不帶後綴 * @param response * @throws IOException * @author geYang * @date 2018-06-13 10:30 * */ public static void download(String fileUrl, String fileName ,HttpServletResponse response) throws IOException { // 文件下載 byte[] downloadByte = getDownloadByte(fileUrl); if (downloadByte == null) { throw new SysException("下載文件不存在"); } int downloadByteLength = downloadByte.length; response.reset(); fileName = URLEncoder.encode(fileName.trim()+getSuffix(fileUrl), "UTF-8"); response.setHeader("Content-Disposition", "attachment; filename=\""+ fileName +"\""); response.addHeader("Content-Length", String.valueOf(downloadByteLength)); response.setContentType("application/octet-stream; charset=UTF-8"); IOUtils.write(downloadByte, response.getOutputStream()); } }
2, 訪問文件時 spring boot 路徑處理:bash
/** * WebMvc配置 * @author geYang * @date 2018-05-14 */ @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // 上傳圖片訪問目錄: 若是有權限攔截的話,應該將 (UploadUtils.BASE_URL + "**") 放行 registry.addResourceHandler(UploadUtils.BASE_URL + "**").addResourceLocations("file:"+ UploadUtils.getClassFilePath()+ "/" + UploadUtils.BASE_PATH); } }