1、调用方式
String fileName = "http://localhost:9000/path/29002778.jpg";
String downloadUrl = "http:127.0.0.1:8080/common/download/pic?fileName=" + fileName;
2、下载方法
@GetMapping("/common/download/pic")
public void fileDownload(String fileName) {
String originalString = fileName.substring(fileName.lastIndexOf('/') + 1);
HttpURLConnection conn = null;
InputStream inputStream = null;
BufferedInputStream bis = null;
FileOutputStream out = null;
try
{
File file0=new File("D:\\path");
if(!file0.isDirectory()&&!file0.exists()){
file0.mkdirs();
}
out = new FileOutputStream(file0 + "\\" + originalString);
URL httpUrl = new URL(fileName);
conn=(HttpURLConnection) httpUrl.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.connect();
inputStream=conn.getInputStream();
bis = new BufferedInputStream(inputStream);
byte b [] = new byte[1024];
int len = 0;
while((len=bis.read(b))!=-1){
out.write(b, 0, len);
}
System.out.println("下载完成...");
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(out!=null){
out.close();
}
if(bis!=null){
bis.close();
}
if(inputStream!=null){
inputStream.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}