.net传图片 到 javaweb 后台

业务需求:.net写的上位机传图片给javaweb的后台(http请求)

.net 解析图片成base64的数据,以json方式调用java的接口(key:图片名称(1.jap),vale:base64的图片数据)

@PostMapping("/uploadMore")//支持接收多个文件
public AjaxResult uploadMore(@RequestParam(required = false) File file ,@RequestParam(required = false)String msg, HttpServletRequest request) throws Exception {
        StringBuilder buffer = new StringBuilder("");
        request.setCharacterEncoding("UTF-8");
        BufferedReader reader=null;
        reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String line=null;
        while((line = reader.readLine())!=null){
            buffer.append(line);
        }

        //服务器路径
        String path = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+BoardConfig.getFileOneMapping();

        String s = buffer.toString();
        JSONObject jsonObject = JSONObject.parseObject(s);
        if(getKeyValue(jsonObject,path)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

private boolean getKeyValue(JSONObject jsonObject, String path) throws ParseException {
        List list = new ArrayList<>();
        for(Map.Entry entry : jsonObject.entrySet()){
            String key = entry.getKey();
            Object value = entry.getValue();
            //转图片
            boolean b = base64ToFile(value.toString(),key);
            //解析图片名称
            ImageInfo imageInfo = imageInfoService.analyzeImageName(key, path);
            list.add(imageInfo);
        }
        int i = imageInfoMapper.addImage(list);
        if(0==i){
            return false;
        }
        return true;
    }
private boolean base64ToFile(String base64, String fileName) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        String path = BoardConfig.getProfileOne()+ LocalDate.now()+"/"+fileName;
        File file = new File(path);
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        try {
            //由于base64在传输过程中,+和/和=会被替换所以在解码前需要将base64还原成可用的base64
            base64 = base64.replaceAll(" ","+");
            base64 = base64.replaceAll("%2F","/");
            base64 = base64.replaceAll("%3D","=");
            //当使用springMVC时无需使用以上方法进行还原

            byte[] bytes = Base64.decodeBase64(base64.replace("\r\n", ""));
            bis = new BufferedInputStream(new ByteArrayInputStream(bytes));
            bos = new BufferedOutputStream(new FileOutputStream(file));
            byte[] bts = new byte[1024];
            int len = -1;
            while((len = bis.read(bts)) != -1) {
                bos.write(bts,0,len);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
public ImageInfo analyzeImageName(String fileName, String path) throws ParseException {
        ImageInfo imageInfo = new ImageInfo();
        String[] str = fileName.split("_");
        imageInfo.setImageName(fileName);
        imageInfo.setProduceId(str[0]);
        imageInfo.setWorkNum(str[1]);
        imageInfo.setStatusNum(str[2].substring(0,str[2].indexOf(".")));
        imageInfo.setUrl(path+LocalDate.now()+"/"+fileName);
        return imageInfo;
    }

 

你可能感兴趣的:(.net传图片 到 javaweb 后台)