曾经使用过的文件下载方法
2011-03-03 18:13
423 查看
三种方法,都已测试通过。环境:weblogic 10
SysLog sysLog = new SysLog();
sysLog.setPath("d:/osclog/F1_logo.jpg");
sysLog.setName("F1_logo.jpg");
/*
// 下载本地文件
fileName = sysLog.getName(); // 文件名
try {
// 读到流中
InputStream inStream = new FileInputStream(sysLog.getPath());// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=/"" + fileName + "/"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
*/
/*
File file = new File(sysLog.getPath());
try {
InputStream fis = new BufferedInputStream(new FileInputStream(sysLog.getPath()));
byte[] buffer = new byte[fis.available()];//判断流的长度
fis.read(buffer);
fis.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(sysLog.getName().getBytes()));
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException e1) {
System.out.println("error");
e1.printStackTrace();
}
*/
/*
File file = new File("d://osclog//wwsdk.log");
FileInputStream fs = null;
fileName = "wwsdk.log";
try {
fs = new FileInputStream(new File("d:/osclog/wwsdk.log"));
}catch(Exception e) {
e.printStackTrace();
}
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=/"" + fileName + "/"");
int b = 0;
try {
PrintWriter out = response.getWriter();
while((b=fs.read())!=-1) {
out.write(b);
}
fs.close();
out.close();
System.out.println("文件下载完毕.");
}catch(Exception e) {
e.printStackTrace();
System.out.println("下载文件失败!");
}
*/
SysLog sysLog = new SysLog();
sysLog.setPath("d:/osclog/F1_logo.jpg");
sysLog.setName("F1_logo.jpg");
/*
// 下载本地文件
fileName = sysLog.getName(); // 文件名
try {
// 读到流中
InputStream inStream = new FileInputStream(sysLog.getPath());// 文件的存放路径
// 设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=/"" + fileName + "/"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
*/
/*
File file = new File(sysLog.getPath());
try {
InputStream fis = new BufferedInputStream(new FileInputStream(sysLog.getPath()));
byte[] buffer = new byte[fis.available()];//判断流的长度
fis.read(buffer);
fis.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(sysLog.getName().getBytes()));
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException e1) {
System.out.println("error");
e1.printStackTrace();
}
*/
/*
File file = new File("d://osclog//wwsdk.log");
FileInputStream fs = null;
fileName = "wwsdk.log";
try {
fs = new FileInputStream(new File("d:/osclog/wwsdk.log"));
}catch(Exception e) {
e.printStackTrace();
}
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=/"" + fileName + "/"");
int b = 0;
try {
PrintWriter out = response.getWriter();
while((b=fs.read())!=-1) {
out.write(b);
}
fs.close();
out.close();
System.out.println("文件下载完毕.");
}catch(Exception e) {
e.printStackTrace();
System.out.println("下载文件失败!");
}
*/
相关文章推荐
- PHP使用文件流下载文件方法(附:解决下载文件内容乱码问题)
- 使用HttpClient实现文件的上传下载方法
- jacob下载问题, Office word 此文件正由另一应用程序或用户使用的解决方法
- MVC 使用FileStreamResult方法的下载excel和word文件问题
- 微信WeUI设计规范文件下载及使用方法
- Afinal加载网络图片及下载文件使用方法
- 用Delphi实现文件下载的几种方法(三种使用控件的方法)
- ASP.NET中使用C#文件下载四方法
- X度网盘大文件使用浏览器或迅雷下载的方法之一
- 【用Python写爬虫】获取html的方法【四】:使用urllib下载文件
- 使用HttpClient实现文件的上传下载方法
- thinkphp使用http::download下载word或者ppt文件乱码问题的解决方法
- VB.NET中使用FTP下载文件的两种方法
- sharepoint 2010 使用sharepoint脚本STSNavigate方法实现文件下载
- 使用FileZilla从Linux系统下载文件的方法
- VB.NET中使用FTP下载文件的两种方法。
- 和我一起使用Python的bottle来实现文件的上传和下载(使用post方法)
- iOS使用自定义字体的方法(内置和任意下载ttf\otf\ttc字体文件)
- ASP.NET文件下载避免使用WriteFile方法
- PHP使用文件流下载文件方法(附:解决下载文件内容乱码问题)