1、上传base64图片
upload:function () {
var self = this;
var uploads = self.uploadList;
var names = self.fileNames;
for (var i = 0; i < uploads.length; i++) {
var upload = uploads[i];
var name = names[i];
jQuery.ajax({
type: "post",
url: baseUrl + 'xjgl/xjzpgl/plsczp',
data: {upload:upload,name:name,zplx:self.saveform.zplx},
async: true,
success: function (res) {
self.uploadList.splice(self.uploadList.indexOf(upload), 1)
self.fileNames.splice(self.fileNames.indexOf(name), 1)
self.$Notice.success({
title: '文件上传成功',
desc: '文件 ' + name + ' 上传成功。'
});
}
})
}
},
后端 存入的是byte[] 存进数据库的blob类型的字段中
@ResponseBody
@RequestMapping(value = "plsczp" , method = RequestMethod.POST )
public String plsczp(String upload,String name,XjzpPage xjzpPage,@AuthenticationPrincipal UserDetails user,HttpServletRequest request){
String username = user.getUsername();
try {
String[] filenames = name.split("\\.");
String fileType = filenames[filenames.length-1];
String filename = name.substring(0,name.indexOf(fileType)-1);
long timestamp=System.currentTimeMillis();
String jiaminame = filename + "@" + timestamp;
String wjUrl = "D:"+File.separator+"xjzp" +File.separator + "dr";
String bsUrl = wjUrl +File.separator +jiaminame +"." + fileType;
Map<String,String> xsxx = xjzpService.getXsxx(filename);
if(xsxx == null){
throw new Exception();
}
String[] base64 =upload.split(",");
byte[] base64Body = new BASE64Decoder().decodeBuffer(base64[1]);
//byte[] base64Body = new BASE64Decoder().decodeBuffer(upload);
XszpXxEntity xszpXxEntity = new XszpXxEntity();
xszpXxEntity.setPylx("1");
xszpXxEntity.setXjid(xsxx.get("ID"));
xszpXxEntity.setZplxdm(xjzpPage.getZplx());
xszpXxEntity.setZp(base64Body);
xszpXxEntity.setZpbslj(bsUrl);
xszpXxEntity.setZpmc(jiaminame + ".jpg");
InetAddress ip = InetAddress.getLocalHost();
String mac = getLocalMac(ip);
xszpXxEntity.setIp(ip.toString());
xszpXxEntity.setMac(mac);
xszpXxEntity.setCjr(username);
xszpXxEntity.setZhxgr(username);
xjzpService.insertXjXszpxx(xszpXxEntity);
boolean s = GenerateImage(base64[1],bsUrl,wjUrl);
return "1";
}catch (Exception e){
e.printStackTrace();
return "2";
}
}
public boolean GenerateImage(String imgStr, String imgFilePath,String wjUrl) {
if (imgStr == null)// 图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] bytes = decoder.decodeBuffer(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
// 生成jpeg图片
File file = new File(wjUrl);
//如果目录不存在则创建
if(!file.isDirectory()){
file.mkdirs();
}
OutputStream out = new FileOutputStream(new File(imgFilePath));
out.write(bytes);
out.flush();
out.close();
return true;
} catch (Exception e) {
e.getMessage();
return false;
}
}
前端查看照片
@SuppressWarnings("all")
@RequestMapping(value = "ckXszp" , method = RequestMethod.POST )
public Map ckXszp(@AuthenticationPrincipal UserDetails user, XjzpPage xjzpPage,HttpServletRequest request ) throws SQLException, UnsupportedEncodingException {
Map map=new HashMap();
List<Map<String,String>> zplxList = xjzpService.queryZplx();
String xjid = xjzpService.getXjid(xjzpPage);
xjzpPage.setXjid(xjid);
List<XszpXxEntity> zpList = xjzpService.queryZp(xjzpPage);
if(zpList.size()>0){
for (int i = 0; i < zpList.size(); i++) {
XszpXxEntity xszpXxEntity = zpList.get(i);
byte[] bdata = xszpXxEntity.getZp();
BASE64Encoder encode = new BASE64Encoder();
String s = encode.encode(bdata);
s = "data:image/jpeg;base64," + s;
xszpXxEntity.setZpstr(s);
}
}
map.put("zplxList",zplxList);
map.put("zpList",zpList);
return map;
}
2、get方法请求下载导出
@GetMapping(value = "exportPhotos")
@Transactional
public void exportPhotos(XjzpPage xjzpPage,String ids,boolean pldc, HttpServletRequest request, HttpServletResponse response) {
try {
if(pldc == true){
String[] xhs = ids.split(",");
for (int i = 0; i < xhs.length; i++) {
String xh = xhs[i];
xjzpPage.setXh(xh);
List<XjzpXsEntity> xsList = xjzpService.queryBks(xjzpPage);
for (int j = 0; j < xsList.size(); j++) {
XjzpXsEntity xjzpXsEntity = xsList.get(j);
xjzpPage.setXjid(xjzpXsEntity.getId());
List<XszpXxEntity> xszpXxEntity = xjzpService.queryZp(xjzpPage);
String xjid = "";
for (int k = 0; k < xszpXxEntity.size(); k++) {
XszpXxEntity xxEntity = xszpXxEntity.get(k);
xjid += xxEntity.getXjid() + ",";
/*response.setContentType("jpeg/img/jpg/png");
byte[] bytes = xxEntity.getZp();
OutputStream outputStream = response.getOutputStream();
response.setHeader("Content-Disposition", "attachment; filename=" + xxEntity.getZpmc());
InputStream in = new ByteArrayInputStream(bytes);
try {
int len = 0;
byte[] buf = new byte[1024];
while ((len = in.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
if (in != null) {
in.close();
}
}*/
/*BASE64Encoder encode = new BASE64Encoder();
String s = encode.encode(bytes);
String wjUrl = "D:"+File.separator+"xjzp" +File.separator + "dc";
String bsUrl = wjUrl +File.separator + xxEntity.getZpmc();
GenerateImage(s,bsUrl,wjUrl);*/
}
String xjid1 = xjid.substring(0,xjid.length() -1);
String[] xjids = xjid1.split(",");
File path = new File(ResourceUtils.getURL("classpath:").getPath());
File upload = new File(path.getAbsolutePath(), "templates/temp/");
export2zip(request, response, upload,xxEntity.getZpmc());
//删除临时目录文件
File[] tempList = upload.listFiles();
if (tempList != null) {
for (File file : tempList) {
if (file.isFile()) file.delete();
}
}
}
}
}else{
List<XjzpXsEntity> xsList = xjzpService.queryBks(xjzpPage);
}
}catch (Exception e){
e.printStackTrace();
}
}