java写response实现⽂件下载
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.URL;
import java.URLConnection;
semicolon
public abstract class FileService {
/**
*  filename 下载到客户端后的⽂件名称
*  file 待下载的⽂件
* 下载⽂件
*/
public void downloadFileToClient(String filename,  File file, HttpServletResponse response) {
FileInputStream inStream = null;
try {
inStream = new FileInputStream(file);
byte[] buf = new byte[4096];input output error
int readLength;
setResponseHeader(response, filename);
while (((readLength = ad(buf)) != -1)) {
}
}catch (Exception e){
}finally {
try {
inStream.close();
} catch (Exception e) {
}
}
}
/**
*  设置响应头
* @param response
绝世武神* @param fileName ⽂件名称
*/
protected void setResponseHeader(HttpServletResponse response, String fileName) {
String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
String prefix = fileName.substring(0, fileName.lastIndexOf("."));
try {
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ new Bytes("GB2312"), "8859_1")
+ suffix);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ppt超链接样式ex.printStackTrace();
}
}
protected void downloadNetFile(String source, String dest) {
// 下载⽹络⽂件
//  int bytesum = 0;
int byteread = 0;
InputStream inStream = null;
InputStream inStream = null;
FileOutputStream fs = null;
try {
URL url = new URL(source);
URLConnection conn = url.openConnection();  inStream = InputStream();
fs = new FileOutputStream(dest);
byte[] buffer = new byte[inStream.available()]; //  int length;
while ((byteread = ad(buffer)) != -1) { //    bytesum += byteread;
fs.write(buffer, 0, byteread);
}java下载过程
} catch (Exception e) {
} finally {
if (null != inStream) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != fs) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}spring away
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。