使用apache的IOUtils类完成--文件下载(FileDownload)程序

本文提供了一个关于如何使用Java进行文件读写的示例代码,包括从指定路径读取文件内容到字节数组,以及将字节数组写入到新的文件中。通过这个例子,读者可以了解到如何利用Java标准库和Apache Commons IO工具包实现文件操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

博客转自:http://wing123.iteye.com/blog/765308

----------------------

  1. package com.test;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.OutputStream;  
  10.   
  11. import org.apache.commons.io.IOUtils;  
  12. import org.junit.BeforeClass;  
  13. import org.junit.Test;  
  14.   
  15. public class FileItem {  
  16.   
  17.     @BeforeClass  
  18.     public static void setUpBeforeClass() throws Exception {  
  19.     }  
  20.   
  21.     @Test  
  22.     public void test() throws FileNotFoundException {  
  23.     // 模拟从服务器上下载文件  
  24.     String filename = "D:\\ufsoft\\nchome_huaxin\\modules\\hxsale\\file\\termb.lic";  
  25.     byte[] data = getFileByteArray(filename);  
  26.     if (null == data)  
  27.         throw new FileNotFoundException("文件没有找到!");  
  28.   
  29.     // 模拟写入本地磁盘  
  30.     String filename2 = "c:\\termb.lic";  
  31.     createNewFile(filename2, data);  
  32.     }  
  33.   
  34.     /** 
  35.      * 写入本地磁盘 
  36.      *  
  37.      * @param filename 
  38.      * @param data 
  39.      */  
  40.     private void createNewFile(String filename, byte[] data) {  
  41.     File file = new File(filename);  
  42.     OutputStream output = null;  
  43.     try {  
  44.         if (!file.exists())  
  45.         file.createNewFile();  
  46.         output = new FileOutputStream(file);  
  47.         IOUtils.write(data, output);  
  48.     } catch (IOException e) {  
  49.         e.printStackTrace();  
  50.     } finally {  
  51.         if (null != output) {  
  52.         try {  
  53.             output.close();  
  54.         } catch (IOException e1) {  
  55.             e1.printStackTrace();  
  56.         }  
  57.         output = null;  
  58.         }  
  59.     }  
  60.   
  61.     }  
  62.   
  63.     /** 
  64.      * 下载文件 
  65.      *  
  66.      * @param filename 
  67.      * @return 
  68.      */  
  69.     private byte[] getFileByteArray(String filename) {  
  70.     File file = new File(filename);  
  71.     InputStream input = null;  
  72.     byte[] data = null;  
  73.     try {  
  74.         if (!file.exists())  
  75.         return null;  
  76.   
  77.         input = new FileInputStream(file);  
  78.         data = IOUtils.toByteArray(input);  
  79.     } catch (IOException e) {  
  80.         e.printStackTrace();  
  81.     } finally {  
  82.         if (null != input) {  
  83.         try {  
  84.             input.close();  
  85.         } catch (IOException e1) {  
  86.             e1.printStackTrace();  
  87.         }  
  88.         input = null;  
  89.         }  
  90.     }  
  91.     return data;  
  92.     }  
  93.   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值