javaweb下载文件
2016-06-07 10:20
465 查看
//读取文件->写出文件
public static void main(String[] args) {
InputStream in =null;
OutputStream out = null;
try{
File file = new File("c:\\123.doc");
in = new FileInputStream(file);
out = new FileOutputStream("c:\\666.doc");
int len = 0;
byte buffer[] = new byte[1024];
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//web文件下载
public void download() {
InputStream in = null ;
OutputStream out = null;
try{
in = new FileInputStream("c:\\123.doc");
int len =0;
byte[] buffer = new byte[1024];
out = getResponse().getOutputStream();
getResponse().setHeader("Content-Disposition", "attachment;filename=aaa.doc"); //告诉浏览器以什么方式打开文件
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
}catch (Exception e) {
e.printStackTrace();
}
}
//web文件下载(文件名称乱码解决)
public void download() {
InputStream in = null ;
OutputStream out = null;
try{
in = new FileInputStream("c:\\123.doc");
int len =0;
byte[] buffer = new byte[1024];
out = getResponse().getOutputStream(); //将文件写出response的输出流
getResponse().setContentType("text/html;charset=UTF-8");
getResponse().setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode("作业本.doc","UTF-8")); //告诉浏览器以什么方式打开文件 和 文件名乱码解决
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
InputStream in =null;
OutputStream out = null;
try{
File file = new File("c:\\123.doc");
in = new FileInputStream(file);
out = new FileOutputStream("c:\\666.doc");
int len = 0;
byte buffer[] = new byte[1024];
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//web文件下载
public void download() {
InputStream in = null ;
OutputStream out = null;
try{
in = new FileInputStream("c:\\123.doc");
int len =0;
byte[] buffer = new byte[1024];
out = getResponse().getOutputStream();
getResponse().setHeader("Content-Disposition", "attachment;filename=aaa.doc"); //告诉浏览器以什么方式打开文件
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
}catch (Exception e) {
e.printStackTrace();
}
}
//web文件下载(文件名称乱码解决)
public void download() {
InputStream in = null ;
OutputStream out = null;
try{
in = new FileInputStream("c:\\123.doc");
int len =0;
byte[] buffer = new byte[1024];
out = getResponse().getOutputStream(); //将文件写出response的输出流
getResponse().setContentType("text/html;charset=UTF-8");
getResponse().setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode("作业本.doc","UTF-8")); //告诉浏览器以什么方式打开文件 和 文件名乱码解决
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
}catch (Exception e) {
e.printStackTrace();
}
}
相关文章推荐
- Java读取硬件USBKey(简称UKEY)中的SSL证书信息,创建双向SSL认证上下文环境
- jsp页面中实现时间相减,并且在最近添加的新闻后右上角添加标记(涉及struts2标签和jquey)
- 使用IntelliJ IDEA 14和Maven创建java web项目
- java之抽象概念
- Java正则表达式pattern和matches
- JAVA集合框架学习总结
- java 获取mac地址
- springmvc mybatis maven 整合 事例
- java的if else语句入门
- Java 写文件:FileOutputStream
- Java concurrency – CountDownLatch Example
- java之自动装箱拆箱
- Java学习之InputStream中read()与read(byte[] b)
- js生成日志信息及实现java直接调用flume
- Java链接elasticsearch的api
- Struts2基于注解配置Action
- 深入Java关键字null
- java异常处理详解
- Java初学者都必须理解的六大问题
- Spring MVC学习总结(5)——SpringMVC项目关于安全的一些配置与实现方式