日期:2014-05-18 浏览次数:20761 次
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