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);
相关文档:
有人反映看不到源码,发现是CSDN的代码控件有问题。希望CSDN好好改进一下。
暂时先以文本方式发布出来,格式会变样。
import java.util.LinkedList;
public class ProducerConsumer
{
/**
* @param args
*/
public static void main(String[] args)
{
Queue queue = ......
String 与 StringBuffer区别:
String字符串对象是不可变的;StringBuffer是变长和可写的动态字符序列,可以自动地增加空间。
Integer var = new Integer();//有语法错误
Integer var = new Integer(2);//是对的
java的Vector成员是要用elementAt(i)成员函数来获得的,不能用ve[i]得到。
Vector::public Enumeration< ......
JAVA专业术语集
API:Java ApplicationProgrammingInterface API(应用程序接口)是事先写好的代码,
组织到相关包。例如 Applet 和 AWT 包包括建立字体、菜单、按钮的类(CLASS),
全部的Java API被包含在JavaTM 2 Stan ......
C++与Java的语法区别
首先,两个大的不同是主函数和怎样编译的不同,接下来是许多小的区别。
main 函数
C++
//自由浮动的函数
int main( int argc, char* argv[])
{
printf( "Hello, world" );
}
Java
// 每个函数都必须是一个类的一部分;当java <class>运行是一个特定类的主函数会被调 ......
继承:
java只有单继承,不能同时继承多个类
class Animal{}
class Fish extends Aniaml{}
super.(父类中与子类相同的方法)();//super-特殊的变量,用于访问父类中与子类相同的方法,如下
//父类
class Fish extends animal
{
void output()
{
System.out.println("Fish");
&n ......