通过action下载文件并给文件重命名,线程下载文件(一)

2014-11-24 07:48:34 · 作者: · 浏览: 1

public class DownLoadAction {

/**
*
*/
private static final long serialVersionUID = -4011064370081237608L;

private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
//spring配置文件注入存放文件地址
private String fileAddr;

public String getFileAddr() {
return fileAddr;
}

public void setFileAddr(String fileAddr) {
this.fileAddr = fileAddr;
}

public String doDownLoad() throws Exception {
// getResponse().setContentType(CONTENT_TYPE);

// 解决中文乱码问题
// String filename = new
String title=getRequest().getParameter("title");
title= new String(title.getBytes("ISO8859-1"),"UTF-8");
// title=new String(getRequest().getParameter("title").getBytes("ISO-8859-1"));
// title=new String(getRequest().getParameter("title").getBytes("ISO-8859-1"),"GBK");
String gch = getRequest().getParameter("gch");
String downname = gch+".pdf";
String filename = title+".pdf";

// 创建file对象
String fileUrl = fileAddr + downname;
File file = new File(fileUrl);

//如果下载文件不存在则提示用户并且返回
if (!file.exists()) {
getResponse().getWriter().write("<script>alert('该文献暂时无法提供相关下载!!');window.history.back(-1);");
return null;
}

// 读出文件到i/o流
FileReader fis = new FileReader(file);
BufferedReader buff = new BufferedReader(fis);

//判断文件长度是否大于1KB
if (file.length() > 1024) {

// 设置response的编码方式
getResponse().setContentType("application/x-msdownload");

// 写明要下载的文件的大小
getResponse().setContentLength((int) file.length());

// 设置附加文件名,并且解决中文乱码
getResponse().setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(), "iso-8859-1"));

// 从response对象中得到输出流,准备下载
ServletOutputStream myout = getResponse().getOutputStream();

FileInputStream stream=new FileInputStream(file);
byte data[]=new byte[4096];
int size=0;
size=stream.read(data);

// 开始循环写出数据
while (size!=-1) {
myout.write(data,0,size);
size=stream.read(data);
}
// 将写入到客户端的内存的数据,刷新到磁盘
myout.flush();
myout.close();
stream.close();
return null;
} else {
//从文件中读出相关信息
String downUrl = buff.readLine();

//如果信息以“succ”开头则截取后面url地址
if (downUrl.startsWith("succ")) {
downUrl = downUrl.replace("succ", "");

//add:wxj start
// 创建URL
URL url = null;
URLConnection urlconn = null;
try {
url = new URL(downUrl);
urlconn = url.openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
HttpURLConnection httpconn = (HttpURLConnection) urlconn;

// 不等于HTTP_OK说明连接不成功System.out.print("fail");
if ( httpconn.getResponseCode() != HttpURLConnection.HTTP_OK) {
try {
System.out.println("getResponse()="+this.getResponse().getWriter());
this.getResponse().getWriter().write("<script>alert('该文献暂时无法提供相关下载!!');window.history.back(-1);");
} catch (IOException e) {
e.printStackTrace();
}
}else{
getResponse().getWriter().write("<script>window.location.href=\'" + downUrl + "\'");
// 新开一个线程去下载文件到服务器本地,下次再下载的时候可直接从服务器下载
DownLoadThread downThread = new DownLoadThread();
downThread.setDownUrl(downUrl);
downThread.setFileAddr(fileAddr + downname);
downThread.setResponse(getResponse());
new Thread(downThread).start();

}

//add:wx