⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadurlfile.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
字号:
/**
 *    $Id:DownloadURLFile.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.utils.io;


import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
 
 /**
 * Download url file class is used to download a file in a url.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
public class DownloadURLFile{
	
	//source file to be download
	private String m_sourceFile;
	
	//destination file to be stored
	private String m_destFile;
	
	//if desination file above is empty,download the file to this destination buffer
	private byte[] m_destBuf;
	
	public byte[] getBuffer(){
		return m_destBuf;
	}
	
	//download a source file to buffer	
	public DownloadURLFile(String sourceFile){
		m_sourceFile	=sourceFile;
	} 
  	
  	//download a source file to buffer
	public DownloadURLFile(String sourceFile, String destFile){
		m_sourceFile	=sourceFile;
		m_destFile	=destFile;
	} 
	
	//get a download stream from url
	public static InputStream openDownloadStream(String url){
		
		try{
			DownloadURLFile download	=new DownloadURLFile(url);
		
			//download the file to buffer
			download.download();
		
			//open stream
			return new ByteArrayInputStream(download.getBuffer());
		
		}catch(Exception e){
			return null;
		}
	}	
  	
  	//run downloading
  	public void download(){

		try{
		
	
			URL url;				
			URLConnection conn;
				
			//get file size
			url = new URL(m_sourceFile);
      			conn = url.openConnection();


      			int fileSize = conn.getContentLength();
      			conn.getInputStream().close();

		
			//get file stream
			url	=new URL(m_sourceFile);

			InputStream in	=url.openStream();

			DataInputStream inputStream=new DataInputStream(new BufferedInputStream(in));


			byte[] buffer = new byte[1024];
			m_destBuf =new byte[(int)fileSize];
			int bytesRead=0, totalBytesRead=0;

   
			while (totalBytesRead<fileSize) {
				bytesRead	=(int)(fileSize - totalBytesRead);
				if (bytesRead>buffer.length)
					bytesRead =buffer.length;

					
    				bytesRead = inputStream.read(buffer,0,bytesRead);
    				if (bytesRead>0){


    					//copy bytes to destination buffer
    					for (int i=0; i<bytesRead; i++){
    						m_destBuf[totalBytesRead+i]	=buffer[i];	
    					}
    					//outputStream.write(buffer, 0, bytesRead);
    					totalBytesRead +=bytesRead;
    					
    				}
			}

			
			inputStream.close();
			in.close();

		
			//write dest buffer to dest file
			if (m_destFile!=null && !m_destFile.equals("")){

				DataOutputStream outputStream =
        				new DataOutputStream(
        					new BufferedOutputStream(
        						new FileOutputStream(m_destFile)));


				outputStream.write(m_destBuf,0,(int)fileSize);
				outputStream.flush();
				outputStream.close();
			}


		}catch(Exception e){
			e.printStackTrace();
		}
		
		  		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -