下载功能java代码 JAVA版应用下载
java下载方面的代码
你的这段代码有两处让人疑惑。其一、File()这个构造函数,自已查下API
成都创新互联专业为企业提供牡丹网站建设、牡丹做网站、牡丹网站设计、牡丹网站制作等企业网站建设、网页设计与制作、牡丹企业网站模板建站服务,十余年牡丹做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
其二、FileOutputStream,FileOutputStream(String name)创建一个向具有指定名称的文件中写入数据的输出文件流。它只是创建了一个输出流,其中没有具体的值,那么这个值来自哪呢,其就来自于你的输入流。其中FileOutputStream()中只是创建了一个字节输出流,其只能接受字节流的输出,所以它还需要与DataOutputStream()的配合才能完成输出,具体用法你可以查API
求一个JAVA单线程下载器的代码
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
public class DownloadApp {
protected Shell shell;
private ProgressBar progressBar;
int CurrentPosition = 0;
private Label label;
public static void main(String[] args) {
try {
DownloadApp window = new DownloadApp();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(450, 177);
shell.setText("\u6A21\u62DF\u4E0B\u8F7D\u7A0B\u5E8F");
label = new Label(shell, SWT.NONE);
label.setBounds(32, 32, 110, 17);
progressBar = new ProgressBar(shell, SWT.NONE);
progressBar.setBounds(32, 55, 360, 17);
Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
label.setText("开始下载");
Thread wt = new Thread(new MyTask());
wt.start();
}
});
button.setBounds(312, 89, 80, 27);
button.setText("\u5F00\u59CB\u4E0B\u8F7D");
}
class MyTask implements Runnable {
public void run() {
while(true) {
CurrentPosition++;
if(CurrentPosition 100)
break;
Display.getDefault().asyncExec(new Runnable() {
public void run() {
progressBar.setSelection(CurrentPosition);
label.setText("当前进度..." + CurrentPosition + "%");
if(CurrentPosition = 100)
label.setText("下载完成");
}
});
try {
Thread.sleep(100);
} catch(InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
}
java中怎么实现下载功能,最好能有代码
private File uploadify;
private String uploadifyFileName;
public String uploadFile1() throws Exception {
String extName = "";// 扩展名
String newFileName = "";// 新文件名
String nowTime = df.format(new Date());// 当前时间
String random = "-" + (Math.round(Math.random() * 9000) + 1000);// 随机函数
String path = "uploads/" + nowTime.substring(0, 6) + "/"
+ nowTime.substring(0, 8) + "/";// 保存路径
String savePath = ServletActionContext.getServletContext().getRealPath(
"");
savePath = savePath.replace("\\", "/");
if (!savePath.substring(savePath.length()).equals("/"))
savePath = savePath + "/";
savePath = savePath + path;
// 获取扩展名
if (uploadifyFileName.lastIndexOf(".") = 0) {
extName = uploadifyFileName.substring(uploadifyFileName
.lastIndexOf("."));
}
newFileName = uploadifyFileName.substring(0,
uploadifyFileName.lastIndexOf("."))
+ nowTime.substring(8) + random + extName;
File file = new File(savePath);
if (!file.exists())
file.mkdirs();
uploadify.renameTo(new File(savePath + newFileName));
/*
* HttpServletResponse response = ServletActionContext.getResponse();
* response.setCharacterEncoding("utf-8");
* response.getWriter().print(uploadifyFileName+"上传成功");
*/
String ctx = Struts2Utils.getRequest().getContextPath();
Struts2Utils.renderText(ctx + "/" + path + newFileName);
Wenjdetail detail = new Wenjdetail();
String pt = path + newFileName;
detail.setName(uploadifyFileName);
detail.setUrl(pt);
wenjdetailManager.saveWenjdetail(detail);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.getWriter().print("," + detail.getId());
return null; // 这里不需要页面转向,所以返回空就可以了
}
网站标题:下载功能java代码 JAVA版应用下载
文章来源:http://myzitong.com/article/hhccsh.html