工作中遇到这样的需求:
1、A项目提供下载服务
2、B项目封装A项目的下载服务
3、通过B项目来下载文件。
遇到的场景是:只有A服务可以下载文件,这里ip 限制,其他的服务器无法直接下载文件,这里类似下载请求,B服务无法直接下载文件,只能先请求A服务,通过A服务实现下载。(这里不讲sendredirect)。
A服务,这里对应下载的服务器端,代码如下:
//服务器端
@RequestMapping(value = "/realdwonloadFile", method = RequestMethod.POST)
public void processDownload(HttpServletRequest request, HttpServletResponse response){
int BUFFER_SIZE = 100000;
InputStream in = null;
OutputStream out = null;
System.out.println("Come on, baby .......");
try{
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
String fileName = request.getHeader("fileName");
System.out.println("fileName:" + fileName);
File file = new File(fileName);
response.setContentLength((int) file.length());
response.setHeader("Accept-Ranges", "bytes");
int readLength = 0;
in = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
out = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[BUFFER_SIZE];
while ((readLength=in.read(buffer)) > 0) {
byte[] bytes = new byte[readLength];
System.arraycopy(buffer, 0, bytes, 0, readLength);
out.write(bytes);
}
out.flush();
response.addHeader("token", "hello 1");
}catch(Exception e){
e.printStackTrace();
response.addHeader("token", "hello 2");
}finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
B服务,这里是客户端。代码如下:
/**
*
* @param request
* @param response
* @param urlPath 调用访问下载文件的url(方法)
* @param remoteFilePath 需要下载的文件所在路径
*/
public void clientDown(HttpServletRequest request, HttpServletResponse response,
String urlPath, String remoteFilePath){
DefaultHttpClient httpClient = new DefaultHttpClient();
OutputStream out = null;
InputStream in = null;
// urlPath = "http://localhost:9990/realdown/realdwonloadFile";
// String remoteFilePath = "D:\\usr\\local\\ciecc\\testdown\\";
String fileName =request.getParameter("name");
try {
HttpPost httpPost = new HttpPost(urlPath);
httpPost.addHeader("fileName", remoteFilePath+fileName);
httpPost.setHeader("Content-Disposition", "attachment;filename="+remoteFilePath+fileName);
//这里访问server 端(也就是urlPath路径),server下载文件并将文件流回传
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
in = entity.getContent();
long length = entity.getContentLength();
if (length <= 0) {
System.out.println("下载文件不存在!");
return;
}
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
out = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[4096];
int readLength = 0;
while ((readLength=in.read(buffer)) > 0) {
byte[] bytes = new byte[readLength];
System.arraycopy(buffer, 0, bytes, 0, readLength);
out.write(bytes);
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(in != null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(out != null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
这样就可以实现 通过B服务下载文件。