锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
* 鏈湴涓嬭澆
*
* @param response
* @throws FileNotFoundException
*/
public void downloadLocal(HttpServletResponse response)
throws FileNotFoundException {
// 涓嬭澆鏈湴鏂囦歡
String fileName = "Operator.doc".toString(); // 鏂囦歡鐨勯粯璁や繚瀛樺悕
// 璇誨埌嫻佷腑
InputStream inStream = new FileInputStream("c:/Operator.doc");// 鏂囦歡鐨勫瓨鏀捐礬寰?br /> // 璁劇疆杈撳嚭鐨勬牸寮?br /> response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\""
+ fileName + "\"");
// 寰幆鍙栧嚭嫻佷腑鐨勬暟鎹?br /> byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 緗戠粶鏂囦歡涓嬭澆
*
* @param response
* @throws MalformedURLException
*/
public void downloadNet(HttpServletResponse response)
throws MalformedURLException {
// 涓嬭澆緗戠粶鏂囦歡
int bytesum = 0;
int byteread = 0;
URL url = new URL("windine.blogdriver.com/logo.gif");
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream("c:/abc.gif");
byte[] buffer = new byte[1204];
@SuppressWarnings("unused")
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 鏀寔鍦ㄧ嚎鎵撳紑涓嬭澆
*
* @param filePath
* @param response
* @param isOnLine
* @throws Exception
*/
public void downLoad(String filePath, HttpServletResponse response,
boolean isOnLine,String fname) throws Exception {
System.out.println("filePath:" + filePath);
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 闈炲父閲嶈
if (isOnLine) { // 鍦ㄧ嚎鎵撳紑鏂瑰紡
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename="
+ fname);
// 鏂囦歡鍚嶅簲璇ョ紪鐮佹垚UTF-8
} else { // 綰笅杞芥柟寮?br /> response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ fname);
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}