当前位置: 技术问答>java相关
求上传和下载的JSP源代码,高分相送,最好没有bean的
来源: 互联网 发布时间:2015-08-20
本文导语: 急! | 下载: package learn.load; /** * Title: 学习servlet * Description: * Copyright: Copyright (c) 2002 * Company: * @author * @version 1.0 */ import java.io.*; public class FileDow...
急!
|
下载:
package learn.load;
/**
* Title: 学习servlet
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
import java.io.*;
public class FileDownLoad {
public FileDownLoad() {
}
public void fileDown(String aFileName,javax.servlet.http.HttpServletResponse response) throws Exception {
java.io.BufferedInputStream iin;
BufferedOutputStream dout;
try {
File ff=new File(aFileName);
if(!ff.exists()){
throw new Exception("对不起! 您下载的文件不存在");
}else{
byte[] buffer;
int length=(new Long(ff.length())).intValue();
buffer=new byte[length];
try{
iin=new BufferedInputStream(new java.io.FileInputStream(ff));
//设置类型和头信息
response.setContentType( "application/octet-stream" ); // MIME type for pdf doc
int pos=aFileName.lastIndexOf("\");
aFileName=aFileName.substring(pos+1);
pos=aFileName.lastIndexOf(".");
aFileName="download"+aFileName.substring(pos);
response.setHeader("Content-disposition", "attachment; filename=" +aFileName);
//传送数据
dout = new BufferedOutputStream( response.getOutputStream());
int once = 0;
int total = 0;
while ((total=0)) {
once = iin.read(buffer,total,length);
total += once;
dout.write(buffer,0,length);
}
if(iin!=null){ iin.close();}
if(dout!=null){ dout.close();}
}catch(Exception ex){
throw new Exception("文件下载过程中出现错误! ");
}
}
}catch(Exception ex) {
throw new Exception("文件下载过程中出现错误! ");
}
}
}
JSP页面:
fileDownLoad
文件名:
package learn.load;
/**
* Title: 学习servlet
* Description:
* Copyright: Copyright (c) 2002
* Company:
* @author
* @version 1.0
*/
import java.io.*;
public class FileDownLoad {
public FileDownLoad() {
}
public void fileDown(String aFileName,javax.servlet.http.HttpServletResponse response) throws Exception {
java.io.BufferedInputStream iin;
BufferedOutputStream dout;
try {
File ff=new File(aFileName);
if(!ff.exists()){
throw new Exception("对不起! 您下载的文件不存在");
}else{
byte[] buffer;
int length=(new Long(ff.length())).intValue();
buffer=new byte[length];
try{
iin=new BufferedInputStream(new java.io.FileInputStream(ff));
//设置类型和头信息
response.setContentType( "application/octet-stream" ); // MIME type for pdf doc
int pos=aFileName.lastIndexOf("\");
aFileName=aFileName.substring(pos+1);
pos=aFileName.lastIndexOf(".");
aFileName="download"+aFileName.substring(pos);
response.setHeader("Content-disposition", "attachment; filename=" +aFileName);
//传送数据
dout = new BufferedOutputStream( response.getOutputStream());
int once = 0;
int total = 0;
while ((total=0)) {
once = iin.read(buffer,total,length);
total += once;
dout.write(buffer,0,length);
}
if(iin!=null){ iin.close();}
if(dout!=null){ dout.close();}
}catch(Exception ex){
throw new Exception("文件下载过程中出现错误! ");
}
}
}catch(Exception ex) {
throw new Exception("文件下载过程中出现错误! ");
}
}
}
JSP页面:
fileDownLoad
文件名: