//文件下载 (本域 及 跨域文件下载)
public void downAnnex(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, int systemMark ,String annexPath) throws Exception {
response.setCharacterEncoding("utf-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
String filepath = "";
String kidRootPath = "http://192.168.18.55:8888/kid/";//目标域的 域名
if(systemMark == 1){//本服务哭器下载文件
filepath = servlet.getServletContext().getRealPath("/" + annexPath);
File uploadFile = new File(filepath);
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
response.setHeader("Content-disposition","attachment;filename=" +URLEncoder.encode(annexPath.substring(annexPath.lastIndexOf("/")+1), "utf-8"));
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
}else{//跨域下载文件
filepath = kidRootPath + annexPath;
HttpURLConnection huc = (HttpURLConnection) new URL(filepath).openConnection();
// 设置连接属性
huc.setDoOutput(true);
huc.setRequestMethod("POST");
huc.setUseCaches(false);
huc.setInstanceFollowRedirects(true);
huc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
huc.connect();
// 往目标servlet中提供参数
OutputStream targetOS = huc.getOutputStream();
targetOS.write(filepath.getBytes());
targetOS.flush();
targetOS.close();
response.setContentType(huc.getContentType());
/*response.setHeader("Cache-Control", huc.getHeaderField("Cache-Control"));
response.setHeader("Pragma", huc.getHeaderField("Pragma"));
response.setHeader("Expires", huc.getHeaderField("Expires"));*/
response.setHeader("Content-disposition","attachment;filename=" +URLEncoder.encode(annexPath.substring(annexPath.lastIndexOf("/")+1), "utf-8"));
OutputStream os = response.getOutputStream();
// 将目标servlet的输入流直接往页面输出
InputStream targetIS = huc.getInputStream();
int r;
while ((r = targetIS.read()) != -1) {
os.write(r);
}
targetIS.close();
os.flush();
os.close();
huc.disconnect();
}
}
public void downAnnex(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, int systemMark ,String annexPath) throws Exception {
response.setCharacterEncoding("utf-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
String filepath = "";
String kidRootPath = "http://192.168.18.55:8888/kid/";//目标域的 域名
if(systemMark == 1){//本服务哭器下载文件
filepath = servlet.getServletContext().getRealPath("/" + annexPath);
File uploadFile = new File(filepath);
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
response.setHeader("Content-disposition","attachment;filename=" +URLEncoder.encode(annexPath.substring(annexPath.lastIndexOf("/")+1), "utf-8"));
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
}else{//跨域下载文件
filepath = kidRootPath + annexPath;
HttpURLConnection huc = (HttpURLConnection) new URL(filepath).openConnection();
// 设置连接属性
huc.setDoOutput(true);
huc.setRequestMethod("POST");
huc.setUseCaches(false);
huc.setInstanceFollowRedirects(true);
huc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
huc.connect();
// 往目标servlet中提供参数
OutputStream targetOS = huc.getOutputStream();
targetOS.write(filepath.getBytes());
targetOS.flush();
targetOS.close();
response.setContentType(huc.getContentType());
/*response.setHeader("Cache-Control", huc.getHeaderField("Cache-Control"));
response.setHeader("Pragma", huc.getHeaderField("Pragma"));
response.setHeader("Expires", huc.getHeaderField("Expires"));*/
response.setHeader("Content-disposition","attachment;filename=" +URLEncoder.encode(annexPath.substring(annexPath.lastIndexOf("/")+1), "utf-8"));
OutputStream os = response.getOutputStream();
// 将目标servlet的输入流直接往页面输出
InputStream targetIS = huc.getInputStream();
int r;
while ((r = targetIS.read()) != -1) {
os.write(r);
}
targetIS.close();
os.flush();
os.close();
huc.disconnect();
}
}