求java http下载代码 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JAVA提供了一些包专门用来下载,也有一些工具也可以实现这一功能,去网上查查 http://calmness.javaeye.com/blog/220075 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; } } 可以参考一下如下实例: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(); }} 一张表里面,设一个主键在加一个外键,把外键指向主键可行? 高手,帮我看看我关于BUTTON摁下没有反应 myeclipse的调试功能 平常你们是如何找资料的? 购物车(jsp和servlet做的) 为什么现在Java现在退化到只能用在服务器编程? Tomcat 的问题! 实体Bean编译出错!! 我用jb2005+jboss3.2.4,试一下 message driver bean .部署不成功,紧呀! 在jsp文件中用pageEncoding 设置了文件编码,当jsp引擎转译该jsp文件的时候,它是如何知道该文件的编码呢 菜鸟问题 jsp导出Excel
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;
}
}