java上傳照片附件

來源:互聯網
上載者:User

標籤:servlet   []   oid   byte   flush   link   logs   root   上傳   

是個小的知識點,但因為一個小細節浪費了大半天的時間才弄好,

@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(" ", "+");//替換Null 字元串為+photo= photo.replace("\n", ""); //置空分行符號photo= photo.replace("\t", "");photo= photo.replace("\r", "");//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); //替換Null 字元串為+        $str = str_replace(‘\n‘, ‘‘,$str);  //置空分行符號        $str = str_replace(‘\t‘, ‘‘,$str);         $str = str_replace(‘\r‘, ‘‘,$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;    }

  

java上傳照片附件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.