|
@ -26,7 +26,11 @@ import javax.servlet.ServletOutputStream; |
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
import java.io.*; |
|
|
import java.io.*; |
|
|
import java.net.URLEncoder; |
|
|
import java.net.URLEncoder; |
|
|
|
|
|
import java.nio.channels.Channels; |
|
|
|
|
|
import java.nio.channels.FileChannel; |
|
|
|
|
|
import java.nio.channels.WritableByteChannel; |
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
|
@RestController |
|
|
@RestController |
|
|
@RequestMapping("/huzhouUploadfileinfo") |
|
|
@RequestMapping("/huzhouUploadfileinfo") |
|
@ -49,35 +53,54 @@ public class HuzhouUploadfileinfoController { |
|
|
path = "excelTemplate"+File.separator+path.substring(path.indexOf("/")+1); |
|
|
path = "excelTemplate"+File.separator+path.substring(path.indexOf("/")+1); |
|
|
ClassPathResource classPathResource = new ClassPathResource(path); |
|
|
ClassPathResource classPathResource = new ClassPathResource(path); |
|
|
fileInputStream =classPathResource.getInputStream(); |
|
|
fileInputStream =classPathResource.getInputStream(); |
|
|
}else{ |
|
|
response.addHeader("content-disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8")+";filename*=UTF-8''"+ URLEncoder.encode(fileName,"UTF-8")); |
|
|
fileInputStream = new FileInputStream(path); |
|
|
response.setContentType("application/octet-stream"); |
|
|
file = new File(path); |
|
|
if(file!=null){ |
|
|
} |
|
|
response.addHeader("Content-Length", "" + file.length()); |
|
|
response.addHeader("content-disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8")+";filename*=UTF-8''"+ URLEncoder.encode(fileName,"UTF-8")); |
|
|
}else{ |
|
|
response.setContentType("application/octet-stream"); |
|
|
response.addHeader("Content-Length", "" + fileInputStream.available()); |
|
|
if(file!=null){ |
|
|
} |
|
|
response.addHeader("Content-Length", "" + file.length()); |
|
|
|
|
|
}else{ |
|
|
|
|
|
response.addHeader("Content-Length", "" + fileInputStream.available()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// response.setContentType("multipart/form-data");
|
|
|
// response.setContentType("multipart/form-data");
|
|
|
BufferedInputStream inputStream =null; |
|
|
BufferedInputStream inputStream =null; |
|
|
try { |
|
|
try { |
|
|
ServletOutputStream outputStream = response.getOutputStream(); |
|
|
ServletOutputStream outputStream = response.getOutputStream(); |
|
|
inputStream = new BufferedInputStream(fileInputStream); |
|
|
inputStream = new BufferedInputStream(fileInputStream); |
|
|
byte[] bytes = new byte[1024]; |
|
|
byte[] bytes = new byte[1024]; |
|
|
int len =inputStream.read(bytes); |
|
|
int len =inputStream.read(bytes); |
|
|
while (len!=-1){ |
|
|
while (len!=-1){ |
|
|
outputStream.write(bytes,0,bytes.length); |
|
|
outputStream.write(bytes,0,bytes.length); |
|
|
outputStream.flush(); |
|
|
outputStream.flush(); |
|
|
len=inputStream.read(bytes); |
|
|
len=inputStream.read(bytes); |
|
|
|
|
|
} |
|
|
|
|
|
}finally { |
|
|
|
|
|
if(inputStream!=null){ |
|
|
|
|
|
inputStream.close(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
}finally { |
|
|
}else{ |
|
|
if(inputStream!=null){ |
|
|
file = new File(path); |
|
|
inputStream.close(); |
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
// 设置响应头
|
|
|
|
|
|
response.setHeader("content-disposition", "attachment;filename=" + |
|
|
|
|
|
URLEncoder.encode(fileName, "UTF-8") + |
|
|
|
|
|
";filename*=UTF-8''" + URLEncoder.encode(fileName, "UTF-8")); |
|
|
|
|
|
response.setContentType("application/octet-stream"); |
|
|
|
|
|
response.setContentLength((int) file.length()); |
|
|
|
|
|
|
|
|
|
|
|
// 使用NIO通道
|
|
|
|
|
|
try (FileChannel fileChannel = new FileInputStream(file).getChannel(); |
|
|
|
|
|
WritableByteChannel outputChannel = Channels.newChannel(response.getOutputStream())) { |
|
|
|
|
|
// 使用transferTo方法直接传输数据
|
|
|
|
|
|
fileChannel.transferTo(0, file.length(), outputChannel); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
throw new RuntimeException("文件下载失败", e); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@GetMapping("/uploacFilePageList") |
|
|
@GetMapping("/uploacFilePageList") |
|
|