上传下载文件--项目中的例子

文件上传与下载实现
本文介绍了一种基于Java的文件上传和下载实现方法,通过将文件转换为字节数组进行存储,并提供下载功能。文章详细展示了如何使用字节流处理文件内容。

//*************************************************上传和下载文件begin
   
    private static final String FILE_ENCODEING = "UTF-8";
    private static final String CONTENT_TYPE = "application/octet-stream";
   
    /**
     * 上传文件
     * @param obj
     */
    public void upload(File upload,String fileName)throws Exception{
        TbSysAttach immpSellAttach = new  TbSysAttach();
        byte[] attachContent = getBytesFromFile(upload);
        immpSellAttach.setAttachContent(attachContent);
        immpSellAttach.setStatusId("0"); //有效标记
        immpSellAttach.setAttachName(fileName);
        sysmngHibernate.saveObj(immpSellAttach);
    }
    /**
     * 文件转化为字节数组
     *
     * 
     */
    public static byte[] getBytesFromFile(File f){
        if (f == null) {
            return null;
        }
        try{
            long starttime = System.currentTimeMillis();
            FileInputStream stream = new FileInputStream(f);
            ByteArrayOutputStream out = new ByteArrayOutputStream(100000);
            //ByteArrayInputStream ins = new ByteArrayInputStream(stream);
            byte[] b = new byte[100000];
            int n;
            int i=0;
            while ((n = stream.read(b)) != -1){
                out.write(b, 0, n);
                System.out.println(i++);
                System.out.println(n);
            }
            System.out.println("------"+b.length);
//            byte[] aimBarry = new byte[];
//            ByteArrayInputStream ins = new ByteArrayInputStream(aimBarry);
           
           
           
            stream.close();
            out.close();
            System.out.println("用时--》"+(System.currentTimeMillis() - starttime));
           
           
            return out.toByteArray();
        } catch (IOException e){
        }
        return null;
    }
   

   
    /**
     * 下载文件
     * @param attachId
     * @param request
     * @param response
     * @throws Exception
     */
    public void downFile(String attachId,HttpServletRequest request,HttpServletResponse response)throws Exception
    {
        String fileName = null;
        byte[] fileContent = null;
        TbSysAttach immpSellAttach = (TbSysAttach)sysmngHibernate.getSession().get(TbSysAttach.class, attachId);
        if(null!=immpSellAttach){
             fileName = immpSellAttach.getAttachName();
             fileContent = immpSellAttach.getAttachContent();
        }
        BufferedOutputStream bos;
        String downFileName = null;
        bos = null;
       
        if(fileContent == null || fileContent.length < 0 || response == null)
            return;
       
        try
        {
            response.setContentType(CONTENT_TYPE);
            if(fileName == null || fileName.equals(""))
                fileName = "anonymous";
            downFileName = URLEncoder.encode(fileName, FILE_ENCODEING);
            if(request.getHeader("User-Agent").indexOf("MSIE 5.5") != -1)
                response.setHeader("Content-disposition", (new StringBuilder("filename=")).append(downFileName).toString());
            else
                response.setHeader("Content-disposition", (new StringBuilder("attachment; filename=")).append(downFileName).toString());
            byte buff[] = fileContent;
            bos = new BufferedOutputStream(response.getOutputStream());
            bos.write(buff);
            bos.flush();
        }
        catch(Exception ex)
        {
           
        }
        finally
        {
            if(bos != null)
            {
                bos.close();
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值