servlet 如下import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DownloadServlet() {
super();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = "下载.zip";
String rname = new String(filename.getBytes("utf-8"),"iso-8859-1");
response.addHeader("Content-Disposition", "attachment;filename="+rname);
response.setContentType("application/octet-stream");
File file = new File("e://test//tb.zip");
InputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[is.available()];
is.read(buffer);
is.close();
OutputStream os = new BufferedOutputStream(response.getOutputStream());
os.write(buffer);
os.flush();
os.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}}
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DownloadServlet() {
super();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = "下载.zip";
String rname = new String(filename.getBytes("utf-8"),"iso-8859-1");
response.addHeader("Content-Disposition", "attachment;filename="+rname);
response.setContentType("application/octet-stream");
File file = new File("e://test//tb.zip");
InputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[is.available()];
is.read(buffer);
is.close();
OutputStream os = new BufferedOutputStream(response.getOutputStream());
os.write(buffer);
os.flush();
os.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}}
解决方案 »
- 在线求高手!!!文件下载
- iframe
- java链接sql server 2000,需要用到的sql语句有4条,用下面形式嵌套的执行得到的结果会正确吗
- Hibernate的session问题
- filter能否打成war包,如何部署?
- 请问struts html:options不要自动设置默认option如何实现。
- jfreechart折线图如何让用不到的横坐标不显示?
- UML的问题?
- Java连接oracle 10g数据库问题
- jbuilder7和weblogic6(sp2)没有装在一台机器,如何配置jbuilder7,在线等
- ConstraintViolationException异常,但没有执行 catch(ConstraintViolationException e)中的代码
- FileSystemXmlApplicationContext找不到applicationContext.xml的问题
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;public class ClientA {
public void service(){
String url = "http://192.168.1.121:7503/download/DownloadServlet";
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
HttpResponse response = client.execute(get);
InputStream is = response.getEntity().getContent();
String localfile = "D://aaa.zip";
File file = new File(localfile);
if(!file.exists()){
file.createNewFile();
}
OutputStream os = new FileOutputStream(file);
int read = 0;
byte[] temp = new byte[1024*1024];
while((read=is.read(temp))>0){
byte[] bytes = new byte[read];
System.arraycopy(temp, 0, bytes, 0, read);
os.write(bytes);
}
os.flush();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
ClientA client = new ClientA();
client.service();
}
}
byte[] buffer = new byte[is.available()];
is.read(buffer);这里会不会有问题,文件大的时候读取不玩数据
byte[] buffer = new byte[is.available()];
is.read(buffer);这里会不会有问题,文件大的时候读取不玩数据该如何修改呢?
文件大了不建议用httpclient,直接ftp拿,不然吞吐量长时间过大的话容易出问题。
文件大了不建议用httpclient,直接ftp拿,不然吞吐量长时间过大的话容易出问题。可以多线程下载,断点下载等
其实这个服务端程序完全可以不要的。
httpClient直接访问 文件的Url即可。 一般的web服务器均支持 断点下载,也就支持多线程下载。
文件大了不建议用httpclient,直接ftp拿,不然吞吐量长时间过大的话容易出问题。可以多线程下载,断点下载等
其实这个服务端程序完全可以不要的。
httpClient直接访问 文件的Url即可。 一般的web服务器均支持 断点下载,也就支持多线程下载。这不是多线程就能解决的问题,好比说你网速最多就200K,你开100个下载进程,总速度也最多是200K,每个平均2K。多线程对服务器吞吐量毫无意义。长时间大数据读写占用服务器的IO,数据量小还可以,数据量大的话肯定会完蛋。
文件大了不建议用httpclient,直接ftp拿,不然吞吐量长时间过大的话容易出问题。可以多线程下载,断点下载等
其实这个服务端程序完全可以不要的。
httpClient直接访问 文件的Url即可。 一般的web服务器均支持 断点下载,也就支持多线程下载。这不是多线程就能解决的问题,好比说你网速最多就200K,你开100个下载进程,总速度也最多是200K,每个平均2K。多线程对服务器吞吐量毫无意义。长时间大数据读写占用服务器的IO,数据量小还可以,数据量大的话肯定会完蛋。这倒是,网速是瓶颈。对于哪种方式都一样。