javaweb下载文件
//讀取文件->寫出文件
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("作業(yè)本.doc","UTF-8")); ? //告訴瀏覽器以什么方式打開文件 和 文件名亂碼解決
??while((len=in.read(buffer))>0){
??out.write(buffer, 0, len);
??}
??}catch?(Exception e) {
??e.printStackTrace();
}
??}
總結
以上是生活随笔為你收集整理的javaweb下载文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: npm下载依赖失败并报错
- 下一篇: [密码学基础][每个信息安全博士生应该知