Browse Source

base64图片解码保存图片。

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

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

1
package com.ekexiu.portal.service;
1
package com.ekexiu.portal.service;
2
2
3
import java.awt.image.BufferedImage;
3
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.ByteArrayOutputStream;
5
import java.io.File;
6
import java.io.File;
10
import java.sql.SQLException;
11
import java.sql.SQLException;
11
import java.util.concurrent.TimeUnit;
12
import java.util.concurrent.TimeUnit;
12
13
14
import javax.imageio.ImageIO;
15
13
import org.jfw.apt.web.annotation.Path;
16
import org.jfw.apt.web.annotation.Path;
14
import org.jfw.apt.web.annotation.operate.Get;
17
import org.jfw.apt.web.annotation.operate.Get;
15
import org.jfw.apt.web.annotation.operate.Post;
18
import org.jfw.apt.web.annotation.operate.Post;
19
import org.jfw.util.exception.JfwBaseException;
22
import org.jfw.util.exception.JfwBaseException;
20
import org.jfw.util.io.IoUtil;
23
import org.jfw.util.io.IoUtil;
21
24
25
import com.ekexiu.portal.util.Base64;
26
22
@Path("/image")
27
@Path("/image")
23
public class ImageService {
28
public class ImageService {
24
	private File tmpPath;
29
	private File tmpPath;
142
		this.defaultResourcePhoto = defaultResourcePhoto;
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
	 * @param id 专家ID
166
	 * @param id 专家ID

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

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
}