简单的文件下载后读取显示,该方法可返回内容的结果集。一般适用于文本文档的下载,以供学习交流。
- /**
- *远程下载文件并读取返回p
- *@paramfilePath文件网络地址如http://www.baidu.com/1.txt
- *@returnString
- */
- publicStringDownAndReadFile(StringfilePath){
- Stringdate=newSimpleDateFormat("yyyy-MM-dd").format(newDate());
- FilesavePath=newFile("D://"+date);//创建新文件
- if(!savePath.exists()){
- savePath.mkdir();
- }
- String[]urlname=filePath.split("/");
- intlen=urlname.length-1;
- Stringuname=urlname[len];//获取文件名
- try{
- Filefile=newFile(savePath+"//"+uname);//创建新文件
- if(file!=null&&!file.exists()){
- file.createNewFile();
- }
- OutputStreamoputstream=newFileOutputStream(file);
- URLurl=newURL(filePath);
- HttpURLConnectionuc=(HttpURLConnection)url.openConnection();
- uc.setDoInput(true);//设置是否要从URL连接读取数据,默认为true
- uc.connect();
- InputStreamiputstream=uc.getInputStream();
- System.out.println("filesizeis:"+uc.getContentLength());//打印文件长度
- byte[]buffer=newbyte[4*1024];
- intbyteRead=-1;
- while((byteRead=(iputstream.read(buffer)))!=-1){
- oputstream.write(buffer,0,byteRead);
- }
- oputstream.flush();
- iputstream.close();
- oputstream.close();
- //读取文件
- StringBufferstrb=newStringBuffer();
- FileInputStreamfs=newFileInputStream(newFile(savePath+"//"+uname));
- InputStreamReaderisr=newInputStreamReader(fs,"UTF-8");
- BufferedReaderbr=newBufferedReader(isr);
- Stringdata="";
- while((data=br.readLine())!=null){
- strb.append(data+"\n");
- }
- br.close();
- fs.close();
- isr.close();
- System.out.println(strb.toString());
- returnstrb.toString();
- }catch(Exceptione){
- System.out.println("读取失败!");
- e.printStackTrace();
- }
- returnnull;
- }
简单的文件下载后读取显示,该方法可返回内容的结果集。一般适用于文本文档的下载,以供学习交流。
- /**
- *远程下载文件并读取返回p
- *@paramfilePath文件网络地址如http://www.baidu.com/1.txt
- *@returnString
- */
- publicStringDownAndReadFile(StringfilePath){
- Stringdate=newSimpleDateFormat("yyyy-MM-dd").format(newDate());
- FilesavePath=newFile("D://"+date);//创建新文件
- if(!savePath.exists()){
- savePath.mkdir();
- }
- String[]urlname=filePath.split("/");
- intlen=urlname.length-1;
- Stringuname=urlname[len];//获取文件名
- try{
- Filefile=newFile(savePath+"//"+uname);//创建新文件
- if(file!=null&&!file.exists()){
- file.createNewFile();
- }
- OutputStreamoputstream=newFileOutputStream(file);
- URLurl=newURL(filePath);
- HttpURLConnectionuc=(HttpURLConnection)url.openConnection();
- uc.setDoInput(true);//设置是否要从URL连接读取数据,默认为true
- uc.connect();
- InputStreamiputstream=uc.getInputStream();
- System.out.println("filesizeis:"+uc.getContentLength());//打印文件长度
- byte[]buffer=newbyte[4*1024];
- intbyteRead=-1;
- while((byteRead=(iputstream.read(buffer)))!=-1){
- oputstream.write(buffer,0,byteRead);
- }
- oputstream.flush();
- iputstream.close();
- oputstream.close();
- //读取文件
- StringBufferstrb=newStringBuffer();
- FileInputStreamfs=newFileInputStream(newFile(savePath+"//"+uname));
- InputStreamReaderisr=newInputStreamReader(fs,"UTF-8");
- BufferedReaderbr=newBufferedReader(isr);
- Stringdata="";
- while((data=br.readLine())!=null){
- strb.append(data+"\n");
- }
- br.close();
- fs.close();
- isr.close();
- System.out.println(strb.toString());
- returnstrb.toString();
- }catch(Exceptione){
- System.out.println("读取失败!");
- e.printStackTrace();
- }
- returnnull;
- }