下载文件

master
shijie 4 years ago
parent 6781c145b2
commit 5b1c22f95e
  1. 35
      dq-financial-guarantee/src/main/java/com/daqing/financial/guarantee/controller/DgApplyAmountInfoController.java

@ -37,8 +37,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.*;
import java.io.IOException;
import java.util.*; import java.util.*;
/** /**
@ -144,6 +143,38 @@ public class DgApplyAmountInfoController implements DgApplyAmountInfoControllerA
return ResponseResult.SUCCESS(list); return ResponseResult.SUCCESS(list);
} }
/**
* 下载单个文件
*
* @param response
* @param fileName
* @return
*/
@RequestMapping("/download")
public String download(HttpServletResponse response, @RequestParam("fileName") String fileName) {
File file = new File(PromptSuccess.FILE_URL_PATH + fileName);
if (file.exists()) {
response.setContentType("application/force-download;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;fileName =" + fileName);
byte[] buffer = new byte[1024];
try (FileInputStream inputStream = new FileInputStream(file);
OutputStream outStream = response.getOutputStream();
BufferedInputStream buffStream = new BufferedInputStream(inputStream);) {
int i = 0;
while ((i = buffStream.read(buffer)) != -1) {
outStream.write(buffer);
outStream.write(buffer, 0, i);
}
return "download success";
} catch (Exception e) {
log.error("download error {}", e.getMessage());
}
}
return null;
}
/** /**
* 删除服务上的文件 * 删除服务上的文件
* @param filePath 路径 * @param filePath 路径

Loading…
Cancel
Save