| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package com.gyee.impala.common.util;
- import com.gyee.impala.common.exception.CustomException;
- import com.gyee.impala.common.result.ResultCode;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.core.io.Resource;
- import org.springframework.core.io.UrlResource;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- import java.net.MalformedURLException;
- import java.net.URLEncoder;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Path;
- import java.util.List;
- @Slf4j
- public class FileUtil {
- /**
- * 写入文件
- *
- * @param fileName
- * @param content
- */
- public static void writeFile(String fileName, String content) {
- BufferedWriter bw = null;
- try {
- File file = new File(fileName);
- if (!file.exists())
- file.createNewFile();
- bw = new BufferedWriter(new FileWriter(file, true));
- String[] list = content.split("\n");
- for(int i = 0; i < list.length; i++){
- bw.write(list[i]);
- bw.write("\n");
- if (i % 500 == 0)
- bw.flush();
- }
- bw.flush();
- } catch (Exception e) {
- log.error(e.getMessage());
- } finally {
- try {
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 写入文件
- *
- * @param path
- * @param fileName
- * @param inputStream
- */
- public static void writeFile(String path, String fileName, InputStream inputStream) {
- OutputStream os = null;
- try {
- byte[] bs = new byte[1024];
- int len;
- File file = new File(path);
- if (!file.exists()) {
- file.mkdirs();
- }
- os = new FileOutputStream(file.getPath() + File.separator + fileName);
- // 开始读取
- while ((len = inputStream.read(bs)) != -1) {
- os.write(bs, 0, len);
- }
- } catch (IOException e) {
- e.printStackTrace();
- log.error(e.getMessage());
- } catch (Exception e) {
- e.printStackTrace();
- log.error(e.getMessage());
- } finally {
- // 完毕,关闭所有链接
- try {
- os.close();
- inputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- log.error(e.getMessage());
- }
- }
- }
- // String -> InputStream
- public static InputStream convertStringToInputStream(String content) {
- InputStream result = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
- return result;
- }
- /**
- * 加载文件
- *
- * @param fileName
- * @return
- */
- public static void download(String fileName, HttpServletResponse response) {
- FileInputStream fis = null;
- BufferedInputStream bis = null;
- try {
- // 获取文件
- File file = new File(fileName);
- // 清空缓冲区,状态码和响应头(headers)
- response.reset();
- // 设置ContentType,响应内容为二进制数据流,编码为utf-8,此处设定的编码是文件内容的编码
- response.setContentType("application/octet-stream;charset=utf-8");
- // 以(Content-Disposition: attachment; filename="filename.jpg")格式设定默认文件名,设定utf编码,此处的编码是文件名的编码,使能正确显示中文文件名
- response.setHeader("Content-Disposition", "attachment;fileName=" + file.getName() + ";filename*=utf-8''" + URLEncoder.encode(file.getName(), "utf-8"));
- // 实现文件下载
- byte[] buffer = new byte[1024];
- fis = new FileInputStream(file);
- bis = new BufferedInputStream(fis);
- // 获取字节流
- OutputStream os = response.getOutputStream();
- int i = bis.read(buffer);
- while (i != -1) {
- os.write(buffer, 0, i);
- i = bis.read(buffer);
- }
- } catch (FileNotFoundException e) {
- throw new CustomException(ResultCode.ERROR_FILE_NO);
- } catch (Exception e) {
- throw new CustomException(ResultCode.ERROR);
- } finally {
- if (bis != null) {
- try {
- bis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
|