/**
* Http 文件下载
*/
public static void downloadFile(String httpUrl,String fileName,
HttpServletRequest request, HttpServletResponse response) {
try {
String suffix = fileName.substring(fileName.lastIndexOf("."));
response.reset(); // 必要地清除response中的缓存信息
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;filename=" + (System.currentTimeMillis() + suffix));
System.out.println("httpUrl:"+httpUrl);
System.out.println("fileName:"+fileName);
URI uri = new URI(httpUrl,false,"UTF-8");
HttpClient hc = new HttpClient();
GetMethod get = new GetMethod(uri.toString());
int status = hc.executeMethod(get);
if (status == 200) {
BufferedInputStream bis = new BufferedInputStream(get
.getResponseBodyAsStream());
ServletOutputStream sos = response.getOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bis.read(buffer)) != -1) {
sos.write(buffer, 0, len);
}
sos.flush();
sos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
* Http 文件下载
*/
public static void downloadFile(String httpUrl,String fileName,
HttpServletRequest request, HttpServletResponse response) {
try {
String suffix = fileName.substring(fileName.lastIndexOf("."));
response.reset(); // 必要地清除response中的缓存信息
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;filename=" + (System.currentTimeMillis() + suffix));
System.out.println("httpUrl:"+httpUrl);
System.out.println("fileName:"+fileName);
URI uri = new URI(httpUrl,false,"UTF-8");
HttpClient hc = new HttpClient();
GetMethod get = new GetMethod(uri.toString());
int status = hc.executeMethod(get);
if (status == 200) {
BufferedInputStream bis = new BufferedInputStream(get
.getResponseBodyAsStream());
ServletOutputStream sos = response.getOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bis.read(buffer)) != -1) {
sos.write(buffer, 0, len);
}
sos.flush();
sos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}