import com.alibaba.fastjson.util.IOUtils; import lombok.SneakyThrows; import org.apache.commons.lang3.StringUtils; import org.springframework.http.MediaType; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.nio.file.Paths; import java.time.LocalDate; /** * 文件上传工具类 * * @author zz */ public class FileUtils { /** * 默认上传的地址 */ private static String defaultBaseDir = "/home/contract/upload"; /** * 文件上传 * * @param baseDir 相对应用的基目录 * @param file 上传的文件 * @return 返回上传成功的文件名 */ @SneakyThrows public static String upload(String baseDir, MultipartFile file) { if (StringUtils.isBlank(baseDir)) { baseDir = defaultBaseDir; } String fileName = file.getOriginalFilename(); File desc = new File(baseDir + File.separator + LocalDate.now() + fileName); if (!desc.exists()) { if (!desc.getParentFile().exists()) { desc.getParentFile().mkdirs(); } } String absPath = desc.getAbsolutePath(); file.transferTo(Paths.get(absPath)); return absPath; } /** * 文件下载 * @param response * @param downloadPath * @return */ @SneakyThrows public static void download(HttpServletResponse response, String downloadPath) { String downloadName = StringUtils.substringAfterLast(downloadPath, "/"); if (StringUtils.isBlank(downloadName)) { downloadName = StringUtils.substringAfterLast(downloadPath, "\\"); } response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); FileUtils.setAttachmentResponseHeader(response, downloadName); FileUtils.writeBytes(downloadPath, response.getOutputStream()); } /** * 写入文件 * @param filePath * @param os * @throws IOException */ public static void writeBytes(String filePath, OutputStream os) throws IOException { FileInputStream fis = null; try { File file = new File(filePath); if (!file.exists()) { throw new FileNotFoundException(filePath); } fis = new FileInputStream(file); byte[] b = new byte[1024]; int length; while ((length = fis.read(b)) > 0) { os.write(b, 0, length); } } catch (IOException e) { throw e; } finally { IOUtils.close(os); IOUtils.close(fis); } } /** * 下载文件名重新编码 * * @param response 响应对象 * @param realFileName 真实文件名 * @return */ public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException { String percentEncodedFileName = percentEncode(realFileName); StringBuilder contentDispositionValue = new StringBuilder(); contentDispositionValue.append("attachment; filename=") .append(percentEncodedFileName) .append(";") .append("filename*=") .append("utf-8''") .append(percentEncodedFileName); response.setHeader("Content-disposition", contentDispositionValue.toString()); } /** * 百分号编码工具方法 * * @param s 需要百分号编码的字符串 * @return 百分号编码后的字符串 */ public static String percentEncode(String s) throws UnsupportedEncodingException { String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString()); return encode.replaceAll("\\+", "%20"); } }
Java本地文件上传工具类
于 2024-04-24 11:04:42 首次发布