Java从web服务器下载文件到本地
/*从服务器中下载文件到本地*/
/*url:文件存放在服务器的地址;target:要保存的路径*/
public String DownloadFile(String url,String target){
URLConnection con=null;
URL theUrl=null;
try {
theUrl=new URL(url);//建立地址
con = theUrl.openConnection();//打开连接
con.setConnectTimeout(30000);
con.connect();//连接
} catch (MalformedURLException e) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
return "给定的URL地址有误,请查看";
}
catch (IOException e) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
return "无法连接到远程机器,请重试!";
}
jLabel5.setText("√");
Process++;
File file = new File(gbl_ParentPath+"/UpdateTemp");
if(file.exists()==false){
file.mkdir();
}
String type = con.getContentType();
if (type != null) {
byte[] buffer = new byte[4 * 1024];
int read;
try {
FileOutputStream os = new FileOutputStream(target);
InputStream in = con.getInputStream();//重定向输入
while ((read = in.read(buffer)) > 0) {//读取输出
os.write(buffer, 0, read);//写入本地文件
}
os.close();
in.close();
jLabel6.setText("√");
Process++;
} catch (FileNotFoundException e) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
return "所要下载的文件不存在!";
}catch (IOException e) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
相关文档:
import lotus.domino.*;
import java.io.*;
import java.util.Vector;
import jxl.*;
/*******************************************************************************
* @saveSQMessageToOracle
* @作者:徐华祥
* @2009年10月28日
* @说明:Execl文件的导入
********** ......
一个java文件中,有且只有一个public类
float ff= 1.3f;(4B)
int 4B;
long 8B;
double 8B;
byte 一字节整型
char 2B
数组
int num[] = new int[3];
int[] num;
num = new int[3];
int [] num = new int[]{1,2,3};
int [][] num;
num = new int[3][4];
//不同长
num = new int[3][];
num[0] = new int[5]; ......
一:理解多线程
多线程是这样一种机制,它允许在程序中并发执行多个指令流,每个指令流都称为一个线程,彼此间互相独立。
线程又称为轻量级进程,它和进程一样拥有独立的执行控制,由操作系统负责调度,区别在于线程没有独立的存储空间,而是和所属进程中的其它线程共享一个存储空间,这使得线程间的通信远较进程简单。
......
cmd
set path=javac所在文件夹(不包括javac)
在任意文件夹下直接使用javac.exe,但当cmd 关闭时,就不再起作用
set classpath=...
作用:在任意文件夹下直接使用.class文件,但当cmd 关闭时,就不瑞起作用
查看:set pathh; set classpath
我的电脑->属性->高级->环境变量设置
用户变量:
变量名:classpat ......