• java上传照片附件


    是个小的知识点,但因为一个小细节浪费了大半天的时间才弄好,

    @ResponseBody
        @RequestMapping("/upLoadJdjdImage")
        public ModelMap upLoadJdjdImage(HttpServletResponse response, ModelMap model,@RequestBody String jsonString){
            String code= "", msg= "";
            boolean success= false;
            try {
                String json= URLDecoder.decode(jsonString, "utf-8");
                JSONObject jsonObj= new JSONObject(json);
                String photo= jsonObj.getString("photo");

    我这边是用java service接收到android端的照片,照片格式为用base64处理的二进制图片,按照一般的思路就是直接接收后,生成一个图片文件,

    然后用流写进数据,

    /**上传照片*/
    	public String upload(String photo, HttpServletResponse response, String savePath) {
    		response.setContentType("text/html");
    		BufferedOutputStream bos = null;
    		File newFile = null;
    		String fileName="";
    		try {
    			File file= new File(savePath);
    			if(!file.exists()){//判断路径下是否有此文件
    				file.mkdir();//创建此抽象路径名指定的目录。
    			}
    			if(photo.length()>0){
    				byte [] by= null;
    				BASE64Decoder base64= new BASE64Decoder();
    				by= base64.decodeBuffer(photo);
    				String newPath= savePath +"\" + System.currentTimeMillis() + ".jpg"; 
    				newFile= new File(newPath);
    				bos= new BufferedOutputStream(new FileOutputStream(newFile));
    				bos.write(by);
    				bos.flush();
    				fileName=newFile.getName();
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally{
    			if (bos != null) {
    				try {
    					bos.close();
    				} catch (IOException e1) {
    					e1.printStackTrace();
    				}
    			}
    		}
    		return fileName;
    	}
    

      然而,看了下路径下面写出的图片,居然是纯黑的,这个服务中也没有异常抛出,在网上查了其他的图片写法,基本上都是一致这样写的

    图片,然后就很懵逼了,后面问了前辈才了解到,这样接收的二进制数据还需要对字符进行处理

                  String json= URLDecoder.decode(jsonString, "utf-8");
    			JSONObject jsonObj= new JSONObject(json);
    			String photo= jsonObj.getString("photo");
    			photo= photo.replace("data:image/png;base64", "");
    			photo= photo.replaceAll("data:image/jpeg;base64", "");
    			photo= photo.replace(" ", "+");//替换空字符串为+
    			photo= photo.replace("
    ", ""); //置空换行符
    			photo= photo.replace("	", "");
    			photo= photo.replace("
    ", "");
    			//photo= photo.replace("/", ""); //过虑转义符
    

      需要替换部分特殊字符,这应该算是一个比较冷的小知识点了,这边记录一下,希望可以帮助后面的同学,最后附属php二进制图片数据

    的处理

     function Img($str,$imgName=null)//base64 生成图片保存
        {   
            if( $imgName ){
                unlink(ROOT_PATH.'/public/uploads/'.$imgName);  //删除原图
            }
            $saveName = request()->time() .rand_string(4). '.jpg'; //图片名生成  
            $str = str_replace(' ', '+', $str); //替换空字符串为+
            $str = str_replace('
    ', '',$str);  //置空换行符
            $str = str_replace('	', '',$str); 
            $str = str_replace('
    ', '',$str);
            $str = stripslashes($str);  //过虑转义符      
            $str = str_replace('data:image/jpeg;base64,', '', $str);
            $str = str_replace('data:image/png;base64,', '', $str);
            $img = base64_decode($str); //解码  
            file_put_contents(ROOT_PATH . '/public/uploads/' . $saveName,$img); //保存图片
            $img_url = '/uploads/'.$saveName;  //生成图片保存路径
            return $img_url;
        }
    

      

  • 相关阅读:
    vue 如何在循环中 "监听" 的绑定v-model数据
    验证正则表达式,同时验证手机号码和固定电话号码
    vue 图片上传功能
    localStorage使用总结
    vue2.0 父子组件通信 兄弟组件通信
    SVN使用教程总结
    详解SVN 的使用
    彻底理解setTimeout()
    如何实现一个HTTP请求库——axios源码阅读与分析 JavaScript
    2、Zookeeper原理及应用汇总
  • 原文地址:https://www.cnblogs.com/G-yong/p/7714781.html
Copyright © 2020-2023  润新知