Browse Source

base64图片解码保存图片。

zzy.zhiyuan.foxmail 8 years ago
parent
commit
0a7b88642b

+ 19 - 0
src/main/java/com/ekexiu/portal/service/ImageService.java

@ -1,5 +1,6 @@
1 1
package com.ekexiu.portal.service;
2 2
3
import java.awt.image.BufferedImage;
3 4
import java.io.ByteArrayInputStream;
4 5
import java.io.ByteArrayOutputStream;
5 6
import java.io.File;
@ -10,6 +11,8 @@ import java.io.InputStream;
10 11
import java.sql.SQLException;
11 12
import java.util.concurrent.TimeUnit;
12 13
14
import javax.imageio.ImageIO;
15
13 16
import org.jfw.apt.web.annotation.Path;
14 17
import org.jfw.apt.web.annotation.operate.Get;
15 18
import org.jfw.apt.web.annotation.operate.Post;
@ -19,6 +22,8 @@ import org.jfw.util.context.JfwAppContext;
19 22
import org.jfw.util.exception.JfwBaseException;
20 23
import org.jfw.util.io.IoUtil;
21 24
25
import com.ekexiu.portal.util.Base64;
26
22 27
@Path("/image")
23 28
public class ImageService {
24 29
	private File tmpPath;
@ -142,6 +147,20 @@ public class ImageService {
142 147
		this.defaultResourcePhoto = defaultResourcePhoto;
143 148
	}
144 149
	
150
	@Post
151
	@Path("/saveHead")
152
	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);
158
		String fileName=this.headPath+"/"+id+".gif";
159
        File file = new File(fileName);//可以是jpg,png,gif格式    
160
        ImageIO.write(bi, "jpg", file);//不管输出什么格式图片,此处不需改动   
161
		return true;
162
	}
163
	
145 164
	/**
146 165
	 * 判断是否有专家头像
147 166
	 * @param id 专家ID

+ 23 - 0
src/main/java/com/ekexiu/portal/util/Base64.java

@ -0,0 +1,23 @@
1
package com.ekexiu.portal.util;
2

3
import java.awt.image.BufferedImage;
4
import java.io.ByteArrayInputStream;
5

6
import javax.imageio.ImageIO;
7

8
import sun.misc.BASE64Decoder;
9

10
@SuppressWarnings("restriction")
11
public class Base64 {
12
	public static BufferedImage getBase64(String base64) {
13
		BASE64Decoder decoder = new sun.misc.BASE64Decoder();
14
		try { 
15
			byte[] bytes = decoder.decodeBuffer(base64);                  
16
	        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);    
17
	        BufferedImage bi =ImageIO.read(bais);
18
			return bi; 
19
		} catch (Exception e) { 
20
			return null; 
21
		} 
22
	}
23
}