JavaIO流读取写入文件,附带远程连接内网服务器

 

乱码,路径问题顺带解决了,重复下载问题也解决了。

Map params = (Map) JSON.parse(data);
        /*用工具类把参数转换后的Map再转换成实体类对象*/
        InfoAttach attach=beanToMapUtils.toBean(InfoAttach.class,params);
        try {
            URL url = new URL(attach.getFileUrl());
            // 打开和URL之间的连接
            URLConnection conn = url.openConnection();
            // 设置通用的请求属性
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(80000);
            conn.setReadTimeout(80000);
            //Post 请求不能使用缓存
            conn.setUseCaches(false);
            //调用URLConnection对象提供的connect方法连接远程服务
            conn.connect();

            System.out.println("object:"+conn.getContent());

            //通过URL打开了通道,拿到了输入流对象
            InputStream inStream = conn.getInputStream();
            //创建缓冲流对象加强功能
            //BufferedInputStream bis =new BufferedInputStream(inStream);

            int bytesum = 0;
            int byteread = 0;
            //byte[] buffer = new byte[1204];
            int count=conn.getContentLength();
            byte[] bytes=new byte[count];
            int readCount= 0;
            while (readCount<count){
                  readCount += inStream.read(bytes,readCount,count-readCount);
            }
            //bis.read(bytes);

            attach.setFileSize(conn.getContentLength());
            attach.setBytes(bytes);
            //关闭流
            //bis.close();
            inStream.close();

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

        return InterfaceResult.success(attach);

String fileName = attach.getPk_info() + "-" + attach.getFileName();
        //String filePath = Paths.get(rootDirectory,configProperties.getDownloadDir(), fileName).toString();

        String classpath = this.getClass().getResource("/").getPath().replaceFirst("/", "");
        String docRoot = classpath.replaceAll("/target/classes/", configProperties.getDownloadDir());
        String filePath=docRoot+ "/" + fileName;

        //1.指定要写到的文件目录及名称
        File dest = new File(filePath);
                //System.out.println(dest.getCanonicalPath());
        if (dest.exists()) {
            FileInputStream fis = new FileInputStream(dest);
            attach.setFileSize(fis.available());
            fis.close();
            return;
        }

        /*URL url = new URL(attach.getFileUrl());
        URLConnection conn = url.openConnection();
        InputStream inStream = conn.getInputStream();*/
        /*调用jt端,先把对象转换成map,然后当参数*/
        Map<String,Object> dataMap=new HashMap<>();
        dataMap=beanToMapUtils.toMap(attach);
        String interfaceResult=mobileOAInterfaceService.oaInterfaceAccess(dataMap,"downloadFromNc");
        /*返回的字符串里有个data*/
        //InterfaceResult resultZh=JSON.parseObject(interfaceResult,InterfaceResult.class);
        //String daiZhuan=JSON.toJSONString(resultZh.getData());
        InfoAttach infoAttachZh=JSON.parseObject( interfaceResult ,InfoAttach.class);

        //2.创建文件读入流对象
        OutputStream fos =new FileOutputStream(dest);
        fos.write(infoAttachZh.getBytes());

        //3.创建缓冲流对象加强fos功能
        //BufferedOutputStream bos=new BufferedOutputStream(fos);
        //4.向流中写入数据
        //byte[] bytes=infoAttachZh.getFileInputStreamObj().getBytes();
        //byte[] bytes=infoAttachZh.getBytes();

        //5.刷新和关闭流
        //bos.flush();
        //bos.close();
        fos.close();

        return;
//1.指定要写到的文件目录及名称,filePath就是索引,文件夹路径+文件名字
File dest = new File(filePath);
   System.out.println(dest.getCanonicalPath());
if (dest.exists()) {
    FileInputStream fis = new FileInputStream(dest);
    attach.setFileSize(fis.available());
    fis.close();
    return;
}

 

 

 

转载于:https://my.oschina.net/Skynet01/blog/1793849

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值