求java http下载代码

syq612 2009-11-20 08:54:41
如题
...全文
120 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jabeginner 2009-11-20
  • 打赏
  • 举报
回复
可以参考一下如下实例:

package DownLoad;import java.awt.BorderLayout;import java.awt.FileDialog;import java.awt.Frame;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Image;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import javax.swing.*;public class Dinterface extends JFrame { JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); JPanel p4 = new JPanel(); JPanel p5 = new JPanel(); String urlPath; String savePath; int count; //设置滚动条 JProgressBar Js= new JProgressBar(0,100); JTextArea JT = new JTextArea(15, 20); JLabel J1 = new JLabel("下载地址:"); JTextField JTF1 = new JTextField(20); JLabel J2 = new JLabel("保存位置:"); JTextField JTF2 = new JTextField(14); JButton JB1 = new JButton("另存为"); JButton JB = new JButton("开始下载"); JScrollPane JS = new JScrollPane(JT); JSpinner jsp=new JSpinner(new SpinnerNumberModel(5,0,20,1)); JLabel L = new JLabel("线程数"); public Dinterface() { super("多线程下载器"); JTF1.setText("http://image.stareastnet.com/2006/09/06/20060906160102g1700.mp3"); JB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ButtonAction(e); } }); JB1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ButtonActione(e); } }); p4.setLayout(new GridLayout(2,1)); p1.add(JS); p2.add(J1); p2.add(JTF1); p2.add(Js); p2.add(L); p2.add(jsp); p2.add(JB1); p3.add(J2); p3.add(JTF2); p3.add(JB); p4.add(p2); p4.add(p3); Js.setStringPainted(true); setLayout(new BorderLayout()); add(p1, BorderLayout.NORTH); add(p4, BorderLayout.CENTER); add(p5, BorderLayout.SOUTH); setSize(340, 440); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); setVisible(true); } private void ButtonAction(ActionEvent e) { if (e.getSource() == JB) { //主线程 urlPath=JTF1.getText(); savePath=JTF2.getText(); count=(Integer)(jsp.getValue()); if(urlPath.equals("")){ JT.setText("请输入完整的下载地址"); }else if(savePath.equals("")){ JT.setText("请输入完整的保存地址"); } else { try { BusDown busdown=new BusDown(urlPath,savePath,JT,count,Js); System.out.println(count); Thread t=new Thread(busdown); t.start(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } JT.append("\n"+"寻找服务器资源..."+"\n"); JT.append("\n"+"主线程已启动..."+"\n"); } } } private void ButtonActione(ActionEvent e) { if(e.getSource()==JB1){ urlPath=JTF1.getText(); savePath=JTF2.getText(); Frame f = new Frame("另存为"); FileDialog fd = new FileDialog(f, "另存为", FileDialog.SAVE); int index=urlPath.lastIndexOf("."); String ends=urlPath.substring(index, urlPath.length()); fd.setFile("*"+ends); fd.setVisible(true); try { String savepath = fd.getDirectory(); String savename = fd.getFile(); if (savename != null) { JTF2.setText(savepath+savename); } } catch (Exception esave) { } } } public static void main(String[] args) { new Dinterface(); }}

fhm727 2009-11-20
  • 打赏
  • 举报
回复
response.setContentType("application/x-msdownload");

// 下载显示的文件名
String fileName = (String) request.getParameter("fileName");
// 硬盘存放路径
String filePath = (String) request.getParameter("filePath");

response.addHeader("Content-Disposition", "attachment;filename=" + fileName);

OutputStream outp = null;
FileInputStream fis = null;
boolean isCancelDownload = false;

try {
outp = response.getOutputStream();
fis = new FileInputStream(filePath);

byte[] b = new byte[1024];
int i = 0;

while ((i = fis.read(b)) > 0) {
try {
outp.write(b, 0, i);
} catch (IOException e) {
isCancelDownload = true;
break;
}
}

if (!isCancelDownload) {
outp.flush();
}
out.clear();
out = pageContext.pushBody();
} finally {
if (fis != null) {
fis.close();
fis = null;
}
if (outp != null) {
if (!isCancelDownload) {
outp.close();
}
outp = null;
}
}
invoked 2009-11-20
  • 打赏
  • 举报
回复
zhangdong2009 2009-11-20
  • 打赏
  • 举报
回复
JAVA提供了一些包专门用来下载,也有一些工具也可以实现这一功能,去网上查查
誰伴我闖荡 2009-11-20
  • 打赏
  • 举报
回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧