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 = ......
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>运行是一个特定类的主函数会被调 ......
出处:来源于CSDN ZangXT大虾对某篇关于java中栈与堆的文章的回复
大体分析一下
1. 栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方。与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆。
//栈都是由运行环境来处理的,这点C++和java没有什么不同.对于堆,不过java多了个GC.
2.这里 ......