Browse Source

图片base64解码保存。

zzy.zhiyuan.foxmail 8 years ago
parent
commit
8775ec8ea3
1 changed files with 7 additions and 12 deletions
  1. 7 12
      src/main/java/com/ekexiu/portal/service/ImageService.java

+ 7 - 12
src/main/java/com/ekexiu/portal/service/ImageService.java

@ -1,6 +1,5 @@
1 1
package com.ekexiu.portal.service;
2 2
3
import java.awt.image.BufferedImage;
4 3
import java.io.ByteArrayInputStream;
5 4
import java.io.ByteArrayOutputStream;
6 5
import java.io.File;
@ -11,19 +10,16 @@ import java.io.InputStream;
11 10
import java.sql.SQLException;
12 11
import java.util.concurrent.TimeUnit;
13 12
14
import javax.imageio.ImageIO;
15
16 13
import org.jfw.apt.web.annotation.Path;
17 14
import org.jfw.apt.web.annotation.operate.Get;
18 15
import org.jfw.apt.web.annotation.operate.Post;
19 16
import org.jfw.util.JpgUtil;
20 17
import org.jfw.util.StringUtil;
18
import org.jfw.util.codec.Base64;
21 19
import org.jfw.util.context.JfwAppContext;
22 20
import org.jfw.util.exception.JfwBaseException;
23 21
import org.jfw.util.io.IoUtil;
24 22
25
import com.ekexiu.portal.util.Base64;
26
27 23
@Path("/image")
28 24
public class ImageService {
29 25
	private File tmpPath;
@ -150,14 +146,13 @@ public class ImageService {
150 146
	@Post
151 147
	@Path("/saveHead")
152 148
	public boolean saveImage(String id, String base64) throws IOException {
153
//		BASE64Decoder decoder = new sun.misc.BASE64Decoder();
154
//		byte[] bytes = decoder.decodeBuffer(base64);                  
155
//		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);    
156
//		BufferedImage bi =ImageIO.read(bais);
157
        BufferedImage bi = Base64.getBase64(base64);
149
		Base64 bs64 = new Base64();
150
		byte[] bs = bs64.decode(base64.getBytes("UTF-8"));
158 151
		String fileName=this.headPath+"/"+id+".jpg";
159
        File file = new File(fileName);//可以是jpg,png,gif格式    
160
        ImageIO.write(bi, "jpg", file);//不管输出什么格式图片,此处不需改动 
152
		FileOutputStream fos = new FileOutputStream(fileName);
153
		fos.write(bs);
154
		fos.flush();
155
		fos.close();
161 156
		return true;
162 157
	}
163 158