远程文件服务器文件下载(应用服务器获取远程文件服务器文件输入流)

时间:2024-03-27 10:51:42
在已知远程文件服务器文件的网络路径的情况下
如:
就可以通过new URL(filePath).openConnection().getInputStream() 获取远程文件服务器的文件输入流。
核心代码:
URL url = new URL(filePath);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
urlConn.connect();
BufferedInputStream bis = new BufferedInputStream(urlConn.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
这样有了输入输出流,就可以跟正常下载文件没多大区别了。

远程文件服务器文件下载(应用服务器获取远程文件服务器文件输入流)

远程文件服务器文件下载(应用服务器获取远程文件服务器文件输入流)

详细代码:

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.MalformedURLException;
 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
/**
* 应用服务器获取远程文件服务器文件
* @param request
* @param response
*/
@RequestMapping(value="/fumin/download_companyfiles.htm")
public void download_companyfiles(final HttpServletRequest request,final HttpServletResponse response){
String fileid = request.getParameter("fileid");//group1/M00/00/0F/CgpAcFtHCd2AU4L5ABbeljIeBQ0996.pdf
String fastServerUrl = ConfigManager.INSTANCE.getConfig().getProperty("fastDFS.server");//fastDFS.server=http://webproxydev2.xxx.com:9080/
String fileName= fileid.substring(fileid.lastIndexOf(File.separator)+1);
String filePath = fastServerUrl + fileid;
//文件服务器中文件的网络路径 http://webproxydev2.xxx.com:9080/group1/M00/00/0F/CgpAcFtHCd2AU4L5ABbeljIeBQ0996.pdf
HttpURLConnection urlConn = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
URL url = new URL(filePath);
urlConn = (HttpURLConnection)url.openConnection();
urlConn.connect();
bis = new BufferedInputStream(urlConn.getInputStream());
bos = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream;charset=gb2312");
//MIME区分数据类型:text/html;charset=UTF-8 application/msword text/plain octet-stream 任意的二进制数据
response.setHeader("Content-Disposition","attachment;filename="+fileName);
//当ContentType为image/jpeg,image/png等浏览器支持内嵌打开的类型时 inline表示浏览器内嵌显示一个文件
//attachment表示会下载文件;ContentType=application/octet-stream 默认是下载
//禁止缓存
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
byte[] buffer = new byte[1024];
int readlen = -1;
while ((readlen = bis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, readlen);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
// TODO: handle exception
e.printStackTrace();
logger.error(e.getMessage(), e);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(null!=bos){
bos.close();
}
if(null!=bis){
bis.close();
}
urlConn.disconnect();
} catch (Exception e2) {
// TODO: handle exception
logger.error(e2.getMessage());
e2.printStackTrace();
}
}
}