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);
相关文档:
解决栈和堆内存溢出办法要设置环境内存变量:
1、在运行界面(cmd进入)中运行命令:java -Xms256m -Xmx1024m,维护上下限内存参数,最大内存不能错过系统内存的1/4,最小内存不能小于最大内存的1/4
2、在Tommcat /bin路径下,如果是安装版需要service.bat文件,此文件在解压缩版里。
3、 在运行界面(cmd进入)中
......
JAVA专业术语集
API:Java ApplicationProgrammingInterface API(应用程序接口)是事先写好的代码,
组织到相关包。例如 Applet 和 AWT 包包括建立字体、菜单、按钮的类(CLASS),
全部的Java API被包含在JavaTM 2 Stan ......
一:理解多线程
多线程是这样一种机制,它允许在程序中并发执行多个指令流,每个指令流都称为一个线程,彼此间互相独立。
线程又称为轻量级进程,它和进程一样拥有独立的执行控制,由操作系统负责调度,区别在于线程没有独立的存储空间,而是和所属进程中的其它线程共享一个存储空间,这使得线程间的通信远较进程简单。
......
cmd
set path=javac所在文件夹(不包括javac)
在任意文件夹下直接使用javac.exe,但当cmd 关闭时,就不再起作用
set classpath=...
作用:在任意文件夹下直接使用.class文件,但当cmd 关闭时,就不瑞起作用
查看:set pathh; set classpath
我的电脑->属性->高级->环境变量设置
用户变量:
变量名:classpat ......
Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。
例:
Calenda ......