java http 客户端 服务端 附件传输

背景:做一个溯源秤的系统 做了一个附件的下载到本的秤端系统 称端是springboot ,服务端是nutz(和springboot差不多的国产框架),不多说上代码

客户端代码

/**
 * @title: fileToZip
 * @author: DXW
 * @date: 2019年8月24日 上午 08:41:13
 * @description: 从服务端取附件流存到本地
 * @param:
 * @return:
 */
@ResponseBody
@GetMapping(value = "/fileToZip")
public void  fileToZip(String name, HttpServletResponse response) throws IOException {
    //服务端附件位置 后期可以不要 从服务端表里查
    String param = "F:/webapps/file/one.html/";
    //客户端存放附件位置
    String zipFilePath = "F:/apache-tomcat-6.0/webapps/SOURCE/";
    //服务端取流接口地址 临时
    String filePath = "http://192.168.XXX.XXX:XXX/open/netPay/fileToZips?path=F:/apache-tomcat-6.0/webapps/file/one.html/";
    InputStream inputStream = sendPost(filePath, param);
    String fileName = "ss";
    FileOutputStream fos = null;
    ZipOutputStream zos = null;
    try {
        File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
        if (zipFile.exists()) {
            System.out.println(zipFilePath + "目录下存在名字为:" + fileName + ".zip" + "打包文件.");
        } else {
            fos = new FileOutputStream(zipFile);
            zos = new ZipOutputStream(new BufferedOutputStream(fos));
            byte[] bufs = new byte[1024 * 10];
            // 创建ZIP实体,并添加进压缩包
            ZipEntry zipEntry = new ZipEntry("11");
            zos.putNextEntry(zipEntry);
            // 读取待压缩的文件并写进压缩包里
            int read = 0;
            while ((read = inputStream.read(bufs, 0, 1024 * 10)) != -1) {
                zos.write(bufs, 0, read);
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        // 关闭流
        try {
            if (null != inputStream)
                inputStream.close();
            if (null != zos)
                zos.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

/**
 * @title: sendPost
 * @author: DXW
 * @date: 2019年8月24日 上午 08:42:28
 * @description: http 调取服务端接口 取附件流
 * @param: filePath param
 * @return:
 */
public InputStream sendPost(String filePath, String param) throws IOException {
    PrintWriter out = null;
    BufferedReader in = null;
    String result = "";
    ByteArrayInputStream inputStream = null;
    URL realUrl = new URL(filePath);
    // 打开和URL之间的连接
    URLConnection conn = realUrl.openConnection();
    // 设置通用的请求属性
    conn.setRequestProperty("accept", "*/*");
    conn.setRequestProperty("connection", "Keep-Alive");
    conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
    //发送POST请求必须设置如下两行
    conn.setDoOutput(true);
    conn.setDoInput(true);
    // 获取URLConnection对象对应的输出流
    out = new PrintWriter(conn.getOutputStream());
    out.print(param);
    // flush输出流的缓冲
    out.flush();
    // 定义BufferedReader输入流来读取URL的响应
    in = new BufferedReader(
            new InputStreamReader(conn.getInputStream(),"utf-8"));
    String line;
    while ((line = in.readLine()) != null) {
        result += line;
    }
    Map mapTypes = JSON.parseObject(result);
    String base64String = mapTypes.get("data").toString();
    byte[] bytes = new BASE64Decoder().decodeBuffer(base64String);
    inputStream = new ByteArrayInputStream(bytes);
    return inputStream;
}

服务端代码

/**
 * @title: fileToZips
 * @author: DXW
 * @date: 2019年8月24日 上午 08:29:06
 * @description: 取本地附件 以流传输到客户端
 * @param: path
 * @return:
 */
@At
@Ok("json")
public Result fileToZips(String path) throws IOException {
    File file = null;
    file = new File(path);
    FileInputStream inputStream = new FileInputStream(file);
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    byte[] by = new byte[1024];
    int len = -1;
    while ((len = inputStream.read(by)) != -1) {
        data.write(by, 0, len);
    }
    BASE64Encoder encoder = new BASE64Encoder();
    String strNetImageToBase64 = encoder.encode(data.toByteArray());
    System.out.println(strNetImageToBase64);
    return Result.success("",strNetImageToBase64);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值