注:将中文部分使用UTF-8格式编码,再将%2F替换为/。
/**变量spectrogramPath*/
http://192.168.1.206:9000/mall/upload/brand/other/20250409/720e870c8f504fa79df88ef114674d6f/核磁图谱.pdf
/**
* 下载图谱
* @return
*/
public String downloadSpectrogram(){
HttpServletResponse response = ServletActionContext.getResponse();
OutputStream outputStream;
InputStream inputStream;
String fileName;
try {
fileName = spectrogramPath.substring(spectrogramPath.lastIndexOf("/") + 1);
if(spectrogramPath.startsWith("http")){
//URL url = new URL(spectrogramPath.substring(0,spectrogramPath.lastIndexOf("/") + 1) + URLEncoder.encode(fileName, "UTF-8"));
URL url = new URL(spectrogramPath.substring(0,26) + URLEncoder.encode(spectrogramPath.substring(26), "UTF-8").replace("%2F","/"));
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
inputStream = urlConnection.getInputStream();
}else {
return printMsg("文件不存在!");
}
}else{
File file = new File(com.titan.util.SystemConfig.getInstance().getProperty("NEW_FILE_PATH") + spectrogramPath);
if(file.exists()){
inputStream = new BufferedInputStream(new FileInputStream(file));
}else {
return printMsg("文件不存在!");
}
}
response.reset();
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(), "ISO8859-1"));
response.setContentType("application/octet-stream");
outputStream = new BufferedOutputStream(response.getOutputStream());
int len = 0;
while((len = inputStream.read()) != -1){
outputStream.write(len);
}
outputStream.flush();
inputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}