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
package com.ekexiu.portal.service;
1
package com.ekexiu.portal.service;
2
2
3
import java.awt.image.BufferedImage;
4
import java.io.ByteArrayInputStream;
3
import java.io.ByteArrayInputStream;
5
import java.io.ByteArrayOutputStream;
4
import java.io.ByteArrayOutputStream;
6
import java.io.File;
5
import java.io.File;
11
import java.sql.SQLException;
10
import java.sql.SQLException;
12
import java.util.concurrent.TimeUnit;
11
import java.util.concurrent.TimeUnit;
13
12
14
import javax.imageio.ImageIO;
15
16
import org.jfw.apt.web.annotation.Path;
13
import org.jfw.apt.web.annotation.Path;
17
import org.jfw.apt.web.annotation.operate.Get;
14
import org.jfw.apt.web.annotation.operate.Get;
18
import org.jfw.apt.web.annotation.operate.Post;
15
import org.jfw.apt.web.annotation.operate.Post;
19
import org.jfw.util.JpgUtil;
16
import org.jfw.util.JpgUtil;
20
import org.jfw.util.StringUtil;
17
import org.jfw.util.StringUtil;
18
import org.jfw.util.codec.Base64;
21
import org.jfw.util.context.JfwAppContext;
19
import org.jfw.util.context.JfwAppContext;
22
import org.jfw.util.exception.JfwBaseException;
20
import org.jfw.util.exception.JfwBaseException;
23
import org.jfw.util.io.IoUtil;
21
import org.jfw.util.io.IoUtil;
24
22
25
import com.ekexiu.portal.util.Base64;
26
27
@Path("/image")
23
@Path("/image")
28
public class ImageService {
24
public class ImageService {
29
	private File tmpPath;
25
	private File tmpPath;
150
	@Post
146
	@Post
151
	@Path("/saveHead")
147
	@Path("/saveHead")
152
	public boolean saveImage(String id, String base64) throws IOException {
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
		String fileName=this.headPath+"/"+id+".jpg";
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
		return true;
156
		return true;
162
	}
157
	}
163
	
158