HttpURLConnection上传文件,下载文件

HttpURLConnection上传文件,下载文件

上传文件

1:controller层

    @RequestMapping("/uploadFrImage")
    @ResponseBody
    public JSONObject uploadFrImage(@RequestParam MultipartFile[] files,String uuid, String security,String industry) {
        try {
            String urlStr = "localhost:8080/xxx/uploadFrImage";
            String url = baseUrl+urlStr;
            Map strParams = new HashMap();
            strParams.put("uuid",uuid);
            strParams.put("security",security);
            strParams.put("industry",industry);
            Map<String,InputStream> fileParams = new HashMap<>();
            for(int i=0;i<files.length;i++){
                fileParams.put(files[i].getOriginalFilename(),files[i].getInputStream());
            }
            String str = fasCommonService.uploadFile(url,strParams,fileParams);
            return JSON.parseObject(str);
        } catch (Exception e) {
            logger.error("startOcr Exception [{}]", e.getMessage());
            return null;
        }
    }

2:service层

    //换行
    private static final String LINE_END = "\r\n";
    // 前缀
    private static final String PREFIX = "--";
    // 边界标识 随机生成
    private static final String BOUNDARY = UUID.randomUUID().toString();
    // 编码格式
    private static final String CHARSET = "utf-8";
    // 数据类型
    private static final String CONTENT_TYPE = "multipart/form-data";
    // 超时时间
    private static final int TIME_OUT = 20 * 1000;

    private HttpURLConnection getHttpURLConnection(String actionUrl){
        HttpURLConnection httpURLConnection = null;
        try {
            // 统一资源
            URL url = new URL(actionUrl);
            // http的连接类
            httpURLConnection =(HttpURLConnection) url.openConnection();
            // 设置是否从httpUrlConnection读入,默认情况下是true;
            httpURLConnection.setDoInput(true);
            // 设置是否向httpUrlConnection输出
            httpURLConnection.setDoOutput(true);
            // Post 请求不能使用缓存
            httpURLConnection.setUseCaches(false);
            // 设定请求的方法,默认是GET
            httpURLConnection.setRequestMethod("POST");
            // 读取时间
            httpURLConnection.setReadTimeout(TIME_OUT);
            // 连接时间
            httpURLConnection.setConnectTimeout(TIME_OUT);
            // 设置请求头参数
            // 设置字符编码连接参数
            httpURLConnection.setRequestProperty("Connection
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值