日期:2014-05-18  浏览次数:20701 次

java 文件下载次数问题
做了一个文件下载功能,可是当我点击那个下载链接的时候,文件实际上已经下载了,下载次数加1了,点击下载链接之后,弹出下载对话框,点击下载,那一段下载有关的程序又被执行了一次,下载次数又加1了。
package com.cothink.lsys.action.exam;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class ExamFileAction extends ActionSupport{

private String filename;
private String filetype;
private String fileid;


public InputStream getDownload()throws Exception{ 

filename=java.net.URLDecoder.decode(filename,"utf8");

    HttpServletResponse response=ServletActionContext.getResponse();

OutputStream o=response.getOutputStream();    

byte b[]=new byte[500];    
        
File fileLoad=new  File("F:/LearningSys/trunk/src/LearningSys/WebRoot/downloadmaterial/",fileid+"."+filetype);
        

response.setHeader( "Content-Disposition", "attachment;filename=" + new String( filename.getBytes("gb2312"), "ISO8859-1" )+"."+filetype);

long fileLength=fileLoad.length();
          
String length=String.valueOf(fileLength);

response.setHeader("Content_Length",length);
        

FileInputStream in=new FileInputStream(fileLoad);
        
int n=0;
while((n=in.read(b))!=-1){
String file_str=new String(b);
System.out.println(file_str);
o.write(b,0,n);
}

o.flush();
o.close(); 
in.close(); 
o = null; 
    return in; 
    }
@Override     
    public String execute() {
try {
this.filename= new String(filename.getBytes("iso-8859-1"),"utf-8");
this.filetype= new String(filetype.getBytes("iso-8859-1"),"utf-8");
this.fileid= new String(fileid.getBytes("iso-8859-1"),"utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
FileInputStream in=(FileInputStream)getDownload();
} catch (Exception e) {
e.printStackTrace();
}

ExamMaterial dlm= new ExamMaterial();
System.out.println("downloadTimes begin!!!!!");
dlm.downloadTimes(fileid);

return SUCCESS;
}

public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getFiletype() {
return filetype;
}
public void setFiletype(String filetype) {
this.filetype = filetyp