JAVA 文件下载

public static void main(String[] args) {


String picUrl2 = "http://localhost:8180/test/downFile.act?attachmentName=3.jpg&attachmentId=1407_5ff5cb184a7c04e61c7e15eba367ebba&module=email";

try {
//byte[] picbytes = getUrlFileData(picUrl);
saveUrlFile(picUrl2, "G:\\1.jpg");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

//获取网络文件,转存到fileDes中,fileDes需要带文件后缀名    
    public static void saveUrlFile(String fileUrl,String fileDes) throws Exception    
    {    
        File toFile = new File(fileDes);    
        if (toFile.exists())    
        {    
//          throw new Exception("file exist");    
            return;    
        }    
        toFile.createNewFile();    
        FileOutputStream outImgStream = new FileOutputStream(toFile);    
        outImgStream.write(getUrlFileData(fileUrl));    
        outImgStream.close();    
    }  

//获取链接地址文件的byte数据    
    public static byte[] getUrlFileData(String fileUrl) throws Exception    
    {    
        URL url = new URL(fileUrl);    
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();    
        httpConn.connect();    
        InputStream cin = httpConn.getInputStream();  
        
        
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();    
        byte[] buffer = new byte[1024];    
        int len = 0;    
        while ((len = cin.read(buffer)) != -1) {    
            outStream.write(buffer, 0, len);    
        }    
        cin.close();    
        byte[] fileData = outStream.toByteArray();    
        outStream.close();    
        return fileData;    
    } 
    

你可能感兴趣的:(JAVA 文件下载)