base64字符串还原成原图⽚
今天做⼀个截图上传到ftp服务器的功能,⽤的是jquery插件cropbox.js,后台使⽤的springmvc
@RequestMapping("/saveProductInfo")
public ModelAndView uploadPreviewImage(HttpServletRequest request,AbcTourProduct product,ScheduleVO scheduleVO) throws IOException{
ModelAndView modelAndView=new ModelAndView();
//1.⽂件上传
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
MultipartFile smallMutiFile = File("upload-file");
MultipartFile bigMutiFile = File("upload-file2");
String fileName1= OriginalFilename();
String fileName2= OriginalFilename();
int i1=fileName1.lastIndexOf(".");
String name1=fileName1.substring(0, i1);
String suffix1=fileName1.substring(i1);
String smallName=name1+UUID.randomUUID().toString()+suffix1;
int i2=fileName2.lastIndexOf(".");
String name2=fileName2.substring(0, i2);
String suffix2=fileName2.substring(i2);
String bigName=name2+UUID.randomUUID().toString()+suffix2;
File smallFile=FileUtil.multipartFileToFile(smallMutiFile);
File bigFile=FileUtil.multipartFileToFile(bigMutiFile);
boolean success1=FileUtil.ftpUpload(smallFile, smallName, "/journey/product/");
boolean success2=FileUtil.ftpUpload(bigFile, bigName, "/journey/product/");
if(success1&&success2){
String smallSrc="/journey/product/"+ smallName;
String bigSrc="/journey/product/"+ bigName;
product.setProImgUrl(bigSrc);
product.setProSmallImgUrl(smallSrc);
//新增产品
List<AbcSchedule>scheduleList =ScheduleVOs();
productService.insertSelective(product, scheduleList);
}
return modelAndView;
getsavefilename}
这样做了之后,controller中获得的是原图⽽不是截图,于是我去看插件的实现,发现截图后的图⽚的src是base64的⼀串字符串,⾮常长,如下图只是⼀⼩部分:
然后我就想着怎样把这⼀串的字符串在后台转成图⽚进⾏上传,
做了⼀个⼩栗⼦:
package ller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apachemons.util.Base64;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
@RequestMapping("/saveProductInfo")
public void uploadPreviewImage(String base64Str) throws IOException{
int start = base64Str.indexOf(',') + 1;
base64Str=base64Str.substring(start);
//注意:需要将头部的给去掉
byte[] bytes=Base64.decodeBase64(base64Str);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {// 调整异常数据
bytes[i] += 256;
}
}
OutputStream outputStream=new FileOutputStream("d://aaa");
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
}
对,就是这样,然后再⽤File取这个图⽚,在进⾏上传的相关步骤
注意引⼊的Base64的包,maven坐标是这个
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.7</version>
</dependency>
话说,这个东西花费了我⼤半天的功夫,,,主要原因是在截取去掉base64字符串的头部的的时候多算了⼀位导致⽣成的图⽚⼀直是损坏⽆法打开的,
特此,
mark⼀下
=====================================================================================================
在出问题的时候在技术问了,有两点启发
1.通过base64字符串不能获得原图⽚的后缀,所以巧妙的⽅法是不写后缀,因为图⽚都是在web端查看的,所以不写后缀的话会选择合适的形式来打开
2.另外⼀哥们⼉写过的代码,还没仔细看,先存档⼀下
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论