package DownLoadFile.JspFileDownLoad;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
/**
* JSP FILE DOWNLOAD SUPPORT
*
* @author yufangwen
* @version 1.0
*/
public class JspFileDownload {
/** request object */
private HttpServletResponse response = null;
/** file type: -1 un-setting; 0 normal file; 1 zip file ;2 stream*/
private int fileType = -1;
/** file name to be displayed */
private String disFileName = null;
/** zip file path */
private String zipFilePath = null;
/** file to be zipped */
private String[] zipFileNames = null;
private boolean zipDelFlag = false;
/** file to be downloaded */
private String downFileName = null;
/** error code 0 */
private static final int PROCESS_OK = 0;
/** error code 1 */
private static final int RESPONSE_IS_NULL = 1;
/** error code 2 */
private static final int UNSET_DOWNLOADTYPE = 2;
/** error code 3 */
private static final int UNSET_DIS_FILE_NAME = 3;
/** error code 4 */
private static final int UNSET_DOWN_FILE_NAME = 4;
/** error code 9 */
private static final int IO_EXCEPTION = 9;
/**
* set response object
* @param response response Object
*/
public void setResponse(HttpServletResponse response){
this.response = response;
}
/**
* set file type 0 normal file; 1 zip file ;2 stream
* @param fileType
*/
public void setDownType(int fileType){
this.fileType = fileType;
}
/**
* set display file name
* @param fileName
*/
public void setDisFileName(String fileName){
this.disFileName = fileName;
}
/**
* set zip file path
* @param fileNames
*/
public void setZipFilePath( String path ){
this.zipFilePath = path;
}
/**
* set zip flag
* @param b
*/
public void setZipDelFlag(boolean b){
this.zipDelFlag = b;
}
/**
* set zip file names
* @param fileNames
*/
public void setZipFileNames(String[] fileNames){
this.zipFileNames = fileNames;
}
/**
* set download file name
* @param fileName
*/
public void setDownFileName(String fileName){
this.downFileName = fileName;
}
/**
* set file content
* @param fileContent
*/
public int setFileContent(String fileContent){
try{
byte[] buffs = fileContent.getBytes("UTF-8");
response.getOutputStream().write(buffs);
}catch(IOException e){
return IO_EXCEPTION;
}
return PROCESS_OK;
}
/**
* set file content
* @param fileContent
*/
public int setFileContent(byte[] fileContent){
try{
response.getOutputStream().write(fileContent);
}catch(IOException e){
return IO_EXCEPTION;
}
return PROCESS_OK;
}
/**
* set file content end
*
*/
public int setFileContentEnd(){
try{
response.getOutputStream().close();
}catch(IOException e){
return IO_EXCEPTION;
}
return PROCESS_OK;
}
/**
* main process
* @return
*/
public int process(){
int status = PROCESS_OK;
status = preCheck();
if ( status != PROCESS_OK )
return status;
String fileName = disFileName;
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment;filename=\"" + fileName + "\"");
int BUFSIZE = 1024 * 8;
int rtnPos = 0;
byte[] buffs = new byte[ BUFSIZE ];
FileInputStream inStream = null;
ZipOutputStream zos = null;
InputStream is = null;
String filepath = null;
try{
if ( fileType == 0 || fileType == 1){
if ( fileType == 0 ){
filepath = downFileName;
}else{
filepath = zipFilePath + fileName;
String[] fileToZip = zipFileNames;
zos=new ZipOutputStream(new FileOutputStream(filepath));
ZipEntry ze=null;
byte[] buf=new byte[BUFSIZE];
int readLen=0;
for (int i= 0;i<fileToZip.length;i++){
File f= new File(fileToZip[i]);
ze=new ZipEntry(f.getName());
ze.setSize(f.length());
ze.setTime(f.lastModified());
zos.putNextEntry(ze);
is=new BufferedInputStream(new FileInputStream(f));
while ((readLen=is.read(buf, 0, BUFSIZE))!=-1) {
zos.write(buf, 0, readLen);
}
is.close();
}
zos.close();
}
inStream =new FileInputStream(filepath);
while((rtnPos=inStream.read(buffs)) >0)
response.getOutputStream().write(buffs,0,rtnPos);
response.getOutputStream().close();
inStream.close();
}
if ( zipDelFlag ){
File fToDel = new File(filepath);
fToDel.delete();
}
}catch(IOException e){
return IO_EXCEPTION;
}finally{
try{
if ( inStream != null ){
inStream.close();
inStream = null;
}
if ( zos != null ){
zos.close();
zos = null;
}
if ( is != null ){
is.close();
is = null;
}
}catch (IOException e){
}
}
return status;
}
/**
* 预处理
* @return
*/
private int preCheck(){
if ( response == null )
return RESPONSE_IS_NULL;
if ( disFileName == null || disFileName.trim().length() == 0 )
return UNSET_DIS_FILE_NAME;
if ( fileType == -1 )
return UNSET_DOWNLOADTYPE;
else if ( fileType == 0 ){
if ( downFileName == null || downFileName.trim().length() == 0 )
return UNSET_DOWN_FILE_NAME;
else{
if ( !isFile( downFileName ) )
return UNSET_DOWN_FILE_NAME;
}
}else if ( fileType == 1 ){
if ( zipFilePath == null || zipFilePath.length() == 0 )
return UNSET_DOWN_FILE_NAME;
else{
if ( !isDirect(zipFilePath) )
return UNSET_DOWN_FILE_NAME;
}
if ( zipFileNames == null || zipFileNames.length == 0 )
return UNSET_DOWN_FILE_NAME;
else{
for ( int i=0;i<zipFileNames.length;i++ ){
if ( zipFileNames[i] == null || zipFileNames[i].trim().length() == 0 )
return UNSET_DOWN_FILE_NAME;
else{
if ( !isFi
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于本体的高校毕业生就业综合服务系统 (230个子文件)
all-wcprops 9KB
all-wcprops 7KB
all-wcprops 3KB
all-wcprops 2KB
all-wcprops 1KB
all-wcprops 1KB
all-wcprops 994B
all-wcprops 710B
all-wcprops 710B
all-wcprops 702B
all-wcprops 700B
all-wcprops 694B
all-wcprops 666B
all-wcprops 650B
all-wcprops 648B
all-wcprops 316B
all-wcprops 316B
all-wcprops 316B
all-wcprops 313B
all-wcprops 307B
all-wcprops 304B
all-wcprops 304B
all-wcprops 303B
all-wcprops 303B
all-wcprops 300B
all-wcprops 300B
OntologyManage.class 8KB
JspFileDownload.class 6KB
DbConnection.class 4KB
AdminLoginAction.class 3KB
UserLoginAction.class 3KB
AdminLoginForm.class 801B
UserLoginForm.class 795B
.classpath 2KB
pm.css 12KB
entries 4KB
entries 3KB
entries 2KB
entries 1KB
entries 759B
entries 753B
entries 740B
entries 732B
entries 730B
entries 708B
entries 627B
entries 627B
entries 600B
entries 582B
entries 573B
entries 479B
entries 461B
entries 454B
entries 451B
entries 445B
entries 445B
entries 438B
entries 438B
entries 432B
entries 428B
entries 428B
entries 427B
entries 423B
xx_mid_bg.gif 102KB
logo.gif 5KB
jiuye.gif 2KB
jobprovide.gif 2KB
t_popular.gif 2KB
t_discount.gif 2KB
outschool.gif 1KB
bg1.gif 1KB
inschool.gif 1KB
info_card.gif 987B
more_discount.gif 142B
today_back.gif 138B
icon01.gif 58B
dot.gif 43B
JspFileDownload.java 9KB
OntologyManage.java 7KB
DbConnection.java 4KB
UserLoginAction.java 2KB
AdminLoginAction.java 2KB
AdminLoginForm.java 1KB
UserLoginForm.java 1KB
xiaoxun.jpg 289KB
image1.jpg 37KB
top.jpg 27KB
background.jpg 2KB
homepage.jsp 14KB
JobFileDetail.jsp 8KB
zhaoPinInformation.jsp 8KB
advicejobinformation.jsp 8KB
userpersonalcenter.jsp 8KB
AlterPersonalInf.jsp 8KB
test.jsp 7KB
userLogin.jsp 7KB
graduatestudent.jsp 7KB
JobInfManage.jsp 7KB
graduatestatus.jsp 7KB
jobunit.jsp 7KB
共 230 条
- 1
- 2
- 3
walter-yu
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页