import java.awt.p_w_picpath.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.p_w_picpathio.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"));

return rlt;

} catch (Exception e) {

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

rlt.put("message", "系统异常," + e.getMessage());

return rlt;

}

}


/**

* @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){


try {

//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");

}

} catch (Exception e) {

e.printStackTrace();

param.put("code", "图片上传异常,"+e.getMessage());

}

}


}




案例分析:Android 通过Base64上传图片到服务器