Java后台接收二进制流和Base64数据 您所在的位置:网站首页 二进制流转文件 Java后台接收二进制流和Base64数据

Java后台接收二进制流和Base64数据

2023-10-14 09:22| 来源: 网络整理| 查看: 265

版权声明:本文为CSDN博主「gouyadongGYD」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/gouyadongGYD/article/details/99644173

————————————————

java后台用的是Springmvc框架,接收前台传过来的二进制流数据或者是base64的字符串图片数据。

接收二进制流数据:

@RequestMapping("/uploadImg") @ResponseBody public Object uploadImg(HttpServletRequest request, HttpServletResponse response) throws IOException {     System.out.println("图片上传开始...");     String destDir = "/upload/image";     /* response.setHeader("Access-Control-Allow-Origin", "*");      response.setHeader("Access-Control-Allow-Methods", "POST");*/     ServletInputStream inputStream = request.getInputStream();     //获取文件上传的真实路径     String uploadPath = request.getSession().getServletContext().getRealPath("/");     //保存文件的路径     String filepath = destDir + File.separator + createNewDir();     File destfile = new File(uploadPath + filepath);     if (!destfile.exists()) {         destfile.mkdirs();     }     //文件新名称     String fileNameNew = getFileNameNew() + ".png";     File f = new File(destfile.getAbsoluteFile() + File.separator + fileNameNew);     if (!f.exists()) {         OutputStream os = new FileOutputStream(f);         BufferedOutputStream bos = new BufferedOutputStream(os);         byte[] buf = new byte[1024];         int length;         length = inputStream.read(buf, 0, buf.length);         while (length != -1) {             bos.write(buf, 0, length);             length = inputStream.read(buf);         }         bos.close();         os.close();         inputStream.close();         String lastpath = filepath + File.separator + fileNameNew;         System.out.println("返回图片路径:" + lastpath);                return lastpath;     }     return false; }

接收base64的字符串数据:

/**  * 将接收的base64转换成图片保存  *  * @param imgByte  *            base64数据  * @param cardNum  *            号码  * @return 成功返回图片保存路径,失败返回false  */ @RequestMapping("/saveToImgByStr") @ResponseBody public Object saveToImgByStr(String imgByte,String cardNum,HttpServletRequest request,HttpServletResponse response) {     String destDir = "/upload/image";     /* response.setHeader("Access-Control-Allow-Origin", "*");      response.setHeader("Access-Control-Allow-Methods", "POST");*/     imgByte=imgByte.replaceAll("data:image/png;base64,","");     BASE64Decoder decoder =  new BASE64Decoder();     byte[] imageByte = null;     try{         imageByte = decoder.decodeBuffer(imgByte);         for (int i = 0; i < imageByte.length; ++i) {             if (imageByte[i] < 0) {// 调整异常数据                 imageByte[i] += 256;             }         }     } catch (Exception e) {         e.printStackTrace();     }     if (imageByte.length>0) {         try {             //获取文件上传的真实路径             String uploadPath = request.getSession().getServletContext().getRealPath("/");             //保存文件的路径             String filepath = destDir + File.separator + createNewDir();             File destfile = new File(uploadPath + filepath);             if (!destfile.exists()) {                 destfile.mkdirs();             }             //文件新名称             String fileNameNew = getFileNameNew() + ".png";             File f = new File(destfile.getAbsoluteFile() + File.separator + fileNameNew);             // 将字符串转换成二进制,用于显示图片             // 将上面生成的图片格式字符串 imgStr,还原成图片显示             InputStream in = new ByteArrayInputStream(imageByte);             FileOutputStream fos = new FileOutputStream(f);            // BufferedOutputStream bos = new BufferedOutputStream(fos);             byte[] buf = new byte[1024];             int length;             length = in.read(buf, 0, buf.length);             while (length != -1) {                 fos.write(buf,0,length);                 length = in.read(buf);             }             fos.flush();             fos.close();             in.close();             String lastpath = filepath + File.separator + fileNameNew;             System.out.println("返回图片路径:" + lastpath);               return lastpath;         } catch (Exception e) {             e.printStackTrace();         } finally {         }     }     return false; }

util方法:

/**  * 为文件重新命名,命名规则为当前系统时间毫秒数  *  * @return string  */ private static String getFileNameNew() {     SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");     return fmt.format(new Date()); } /**  * 以当前日期为名,创建新文件夹  *  * @return  */ private static String createNewDir() {     SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");     return fmt.format(new Date()); }

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有