天天看點

App端通過Base64多張圖檔上傳到伺服器,并存入資料庫資料

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import javax.annotation.Resource;

import javax.imageio.ImageIO;

import javax.servlet.http.HttpServletRequest;

import com.shangyu.action.AppBaseController;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.shangyu.core.utils.PropertyUtils;

import com.shangyu.entity.dsz.AppSession;

import com.shangyu.service.dsz.PublicImageService;

import com.shangyu.service.dsz.WorksummaryService;

import com.shangyu.entity.dsz.Worksummary;

import com.shangyu.web.DszUtils;

import sun.misc.BASE64Decoder;

@Controller

@RequestMapping("/app/worksummary")

public class AppWorksummaryController extends AppBaseController{

//shangyu.properties配置檔案圖檔路徑:

//windows存放路徑------->file.path=c:/nginx/img/dsz/

//windows讀取路徑------->imgUrl=http\://115.35.220.220\:8081/dsz/

//linux存放路徑--------->file.path=/usr/local/file_root/dsz/

//linux讀取路徑--------->imgUrl=http\://182.95.5.128\:8081/dsz/

/**

 * 

 * @param formFile ---> android或ios 經過base64加密後傳遞給背景的圖檔拼接字元串

 * @param sessionId

 * @param request

 * @return

 */

@RequestMapping("/save.json")

@ResponseBody

public Map<String, Object> save(String sessionId, String formFile, HttpServletRequest request) {

Map<String, Object> rlt = new HashMap<String, Object>();

Map<String, Object> param = new HashMap<String, Object>();

try {

AppSession session = validateSession(sessionId);

// app未登陸

if (session == null) {

rlt.put("retcode", "-1");

rlt.put("message", "未登陸");

return rlt;

}

Worksummary entity = new Worksummary(session.getSupplierId(), session.getUserId());

worksummaryService.save(entity);

param.put("bizid", entity.getUid());//日志id

param.put("biztype", 2);//圖檔庫類型--->2代表日志類型

//上傳圖檔

if(formFile != null){

uploadImgs(param, formFile);

rlt.put("retcode", "0");

rlt.put("message", "添加成功");

System.out.println(param.get("code"));

} catch (Exception e) {

rlt.put("retcode", "2");

rlt.put("message", "系統異常," + e.getMessage());

 * @param formFile 

    * @Title: uploadImgs

    * @Description: 上傳圖檔

    * @param  map

    * @param  formFile

    * @param  bizid

    * @param  biztype

    * @return void    傳回類型

    * @throws

public void uploadImgs(Map<String, Object> param, String formFile){

//App日志編輯頁面時根據該日志id及圖檔類型删除該日志對應的所有原始圖檔

List<PublicImage> imglist = publicImageService.findByMap(param);

if(imglist.size() > 0){

for (int j = 0; j < imglist.size(); j++) {

publicImageService.delete(imglist.get(j).getBizid());

//拆分多張圖檔進行周遊

String[]  list=formFile.split(",");

for (int i = 0; i < list.length; i++) {

//讀取配置檔案存放圖檔的絕對路徑(上傳的圖檔存放的位置即盤目錄)

String imgPath = PropertyUtils.getPropertyValue("shangyu.properties", "file.path")+"imgs/";

//建立存放圖檔路徑(目錄)

File targetDir = new File(imgPath);

if(!targetDir.exists()){

targetDir.mkdirs();

//建立新的圖檔名(及格式)

String filename = DszUtils.uuid() + ".jpg";

File newfile = new File(imgPath + filename);

BASE64Decoder decoder = new BASE64Decoder();

//通過Base64解密,将圖檔資料解密成位元組數組

byte[] bytes = decoder.decodeBuffer(list[i]);//解密每張圖檔

//構造位元組數組輸入流

ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

//讀取輸入流的資料

BufferedImage bi = ImageIO.read(bais);

//将資料資訊寫進圖檔檔案中

ImageIO.write(bi, "jpg", newfile);// 不管輸出什麼格式圖檔,此處不需改動

bais.close();

//imgs目錄下的圖檔名稱(圖檔相對路徑)

String img = "imgs/"+ filename;

//向圖檔庫添加該日志送出的所有圖檔(儲存圖檔)

PublicImage entity = new PublicImage();

entity.setBiztype((Integer) (param.get("biztype")));//圖檔類型

entity.setBizid((String) param.get("bizid"));//圖檔庫關聯的日志主鍵id

entity.setPath(img);//圖檔相對路徑

entity.setImgname(filename);//圖檔名稱

publicImageService.save(entity);

param.put("code", "success");

e.printStackTrace();

param.put("code", "圖檔上傳異常,"+e.getMessage());

<a href="http://down.51cto.com/data/2368452" target="_blank">附件:http://down.51cto.com/data/2368452</a>

本文轉自 沉澱人生 51CTO部落格,原文連結:http://blog.51cto.com/825272560/1880359