近期写到图片上传,就是将图片上传至服务器
该上传的方式为,前台将图片转成base64码 ,然后将数据流发送后台,然后我再进行base64解码,并且将图片上传至服务器
// DTO层
package com.hand.utils.uploads.dto;
import java.util.List;
/**Auto Generated By Hap Code Generator**/
public class Upload {
private String fileName;//文件名称
private String imgStr;// 图片流
private String[] imgListStr;// 批量上传
private String jsonDataStr;
public String getJsonDataStr() {
return jsonDataStr;
}
public void setJsonDataStr(String jsonDataStr) {
this.jsonDataStr = jsonDataStr;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getImgStr() {
return imgStr;
}
public void setImgStr(String imgStr) {
this.imgStr = imgStr;
}
public String[] getImgListStr() {
imgListStr = this.getImgStr().split(",");
return imgListStr;
}
public void setImgListStr(String[] imgListStr) {
this.imgListStr = imgListStr;
}
}
// Controller 层
package com.hand.utils.uploads.controllers;
import com.hand.hap.system.dto.ResponseData;
import com.hand.utils.uploads.dto.Upload;
import com.hand.utils.uploads.service.IUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
@Controller
public class UploadController{
@Autowired
private IUploadService service;
@RequestMapping(value = "/app/upload/img",method = RequestMethod.POST)
@ResponseBody
public ResponseData appUploadImg(Upload dto, HttpServletRequest request) {
/**
*
* 功能描述: app 上传图片
*
* @auther:lkj
* @date:2018/7/26 下午4:40
* @param:[dto, request]
* @return:com.hand.hap.system.dto.ResponseData
*
*/
try {
ResponseData responseData = service.appUploadImg(dto);
return responseData;
} catch (Exception e) {
e.printStackTrace();
ResponseData responseData = new ResponseData(false);
responseData.setMessage(e.getMessage());
return responseData;
}
}
}
// IUploadService 层
package com.hand.utils.uploads.service;
import com.hand.hap.system.dto.ResponseData;
import com.hand.utils.uploads.dto.Upload;
public interface IUploadService{
/**
*
* 功能描述: APP 图片上传
*
* @auther:lkj
* @date:2018/7/26 下午4:43
* @param:
* @return:
*
*/
ResponseData appUploadImg(Upload dto) throws Exception;
}
// ServiceImpl 实现类
package com.hand.utils.uploads.service.impl;
import com.hand.hap.system.dto.ResponseData;
import com.hand.utils.components.mapper.UtilMapper;
import com.hand.utils.uploads.dto.Upload;
import com.hand.utils.uploads.service.IUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.BASE64Decoder;
import java.io.*;
import java.util.Date;
import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class UploadServiceImpl implements IUploadService {
@Autowired
private UtilMapper utilMapper;
@Override
public ResponseData appUploadImg(Upload dto) throws Exception{
/**
*
* 功能描述: App 图片上传
*
* @auther:lkj
* @date:2018/7/26 下午4:43
* @param:[dto]
* @return:com.hand.hap.system.dto.ResponseData
*
*/
String fileName = dto.getFileName();// 文件夹名称
String[] imgListStr = dto.getImgListStr();
//拼接成完整的指定的文件路径名,创建新文件
//我的文件路径是配置的快码
String sql = "SELECT T.PROFILE_VALUE FROM SYS_PROFILE_VALUE T WHERE ";
sql += "T.PROFILE_ID = ( SELECT PROFILE_ID FROM sys_profile WHERE PROFILE_NAME = 'APP_UP_IMG_PATH' ) ";
String mysql = utilMapper.mysql(sql);
String filePath = mysql+"/"+fileName;
File file = new File(filePath);
if(!file.exists()){
file.mkdirs();
}
for(int i=0;i<imgListStr.length;i++){
// 获取时间戳
long nowDate=(new Date().getTime());
String filename=nowDate+".jpg";
GenerateImage(imgListStr[i],filePath+"/"+filename);
}
return new ResponseData();
}
//解码
public static boolean GenerateImage(String imgStr, String imgFilePath) throws IOException {
// 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) // 图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
// Base64解码
byte[] bytes = decoder.decodeBuffer(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
// 生成jpeg图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(bytes);
out.flush();
out.close();
return true;
}
}