您的位置:首页 > 编程语言 > Java开发

java后台下载服务器端文件 下载其他系统文件

2012-11-12 14:33 393 查看
import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.Writer;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import javax.servlet.ServletOutputStream;

import cn.com.ifc.framework.business.BasicAction;

public class DownLoadAction extends BasicAction {

/**

*

*/

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);</script>");

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);//最大读取4096个字节到缓冲区read,如果stream中没有4096个则读取stream剩下的所有字节,如果一个没有那么size则为-1



// 开始循环写出数据

while (size!=-1) {

myout.write(data,0,size);//从data[off]到data[off+size-1]即data[0至size-1]写入字节到myout输出流中,off+size不得大于data的length.将data写入输出流

size=stream.read(data);//从输入流中给data赋值

}

// 将写入到客户端的内存的数据,刷新到磁盘

myout.flush();

myout.close();

stream.close();

return null;

} else {

//从文件中读出相关信息

String downUrl = buff.readLine();

//如果信息以“succ”开头则截取后面url地址

if (downUrl.startsWith("succ")) {

downUrl = downUrl.replace("succ", "");

downUrl=new String(downUrl.getBytes(), "iso-8859-1");

//add:wxj start

// 创建URL

URL url = null;

URLConnection urlconn = null;

try {

url = new URL(downUrl);

urlconn = url.openConnection();

// System.out.println(urlconn.getContent());

} 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().setContentType(CONTENT_TYPE);

this.getResponse().getWriter().write("<script>alert('该文献暂时无法提供相关下载!!');window.history.back(-1);</script>");

} catch (IOException e) {

e.printStackTrace();

}

}else{

getResponse().getWriter().write("<script>window.location.href=\'" + downUrl + "\'</script>");

// 新开一个线程去下载文件到服务器本地,下次再下载的时候可直接从服务器下载

DownLoadThread downThread = new DownLoadThread();

downThread.setDownUrl(downUrl);

downThread.setFileAddr(fileAddr + downname);

downThread.setResponse(getResponse());

new Thread(downThread).start();



}



//add:wxj end

} else {

this.getResponse().setContentType(CONTENT_TYPE);

getResponse().getWriter().write("<script>alert('该文献暂时无法提供相关下载!!');window.history.back(-1);</script>");

}

return null;

}



}

}



------------------------------------------------------------------------------------

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import javax.servlet.http.HttpServletResponse;

import cn.com.ifc.framework.business.BasicAction;

public class DownLoadThread extends BasicAction implements Runnable {

/**

*

*/

private static final long serialVersionUID = 9116090002778797074L;

//文件地址

private String downUrl;

//存放文件地址

private String fileAddr;

//httpServletResponse

private HttpServletResponse response;

public String getDownUrl() {

return downUrl;

}

public void setDownUrl(String downUrl) {

this.downUrl = downUrl;

}

public String getFileAddr() {

return fileAddr;

}

public void setFileAddr(String fileAddr) {

this.fileAddr = fileAddr;

}



public HttpServletResponse getResponse() {

return response;

}

public void setResponse(HttpServletResponse response) {

this.response = response;

}

@Override

public void run() {

// 服务器返回的状态

int HttpResult = 0;

// 创建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;

try {

HttpResult = httpconn.getResponseCode();

} catch (IOException e) {

e.printStackTrace();

}

//System.out.println(HttpResult);

// 不等于HTTP_OK说明连接不成功System.out.print("fail");

System.out.println("HttpResult="+HttpResult+",downUrl="+downUrl);

if (HttpResult != HttpURLConnection.HTTP_OK) {

try {

System.out.println("getResponse()="+this.getResponse().getWriter());

this.getResponse().getWriter().write("<script>alert('连接不成功,下载失败!!')</script>");

} catch (IOException e) {

e.printStackTrace();

}

} else {*/

// 取数据长度System.out.println(filesize);

// int filesize = urlconn.getContentLength();

BufferedInputStream re = null;

BufferedOutputStream ot = null;

try {

re = new BufferedInputStream(urlconn.getInputStream());

ot = new BufferedOutputStream(new FileOutputStream(fileAddr));

// 创建存放输入流的缓冲

byte[] buffer = new byte[4096];

int num = -1; // 读入的字节数

while ((num = re.read(buffer)) != -1) {

ot.write(buffer, 0, num);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

re.close();

ot.close();

} catch (IOException e) {

e.printStackTrace();

}

}

//}

}

public static void main(String[] args) {

new Thread(new DownLoadThread()).start();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: