ソースを参照

--资源搜索页搜索条件添加认证专家(只有认证专家发布的资源可以显示在搜索列表);保存和更新资源时资源图片多存一张最大尺寸为240的图片。

zzy.zhiyuan.foxmail 8 年 前
コミット
23f8e1e246
共有2 個のファイルを変更した81 個の追加38 個の削除を含む
  1. 64 7
      src/main/java/com/ekexiu/portal/service/ImagesService.java
  2. 17 31
      src/main/java/com/ekexiu/portal/service/ResourceService.java

+ 64 - 7
src/main/java/com/ekexiu/portal/service/ImagesService.java

@ -1,6 +1,9 @@
1 1
package com.ekexiu.portal.service;
2 2

3
import java.awt.geom.AffineTransform;
4
import java.awt.image.AffineTransformOp;
3 5
import java.awt.image.BufferedImage;
6
import java.io.ByteArrayInputStream;
4 7
import java.io.ByteArrayOutputStream;
5 8
import java.io.File;
6 9
import java.io.FileInputStream;
@ -14,6 +17,7 @@ import java.util.List;
14 17
import javax.imageio.ImageIO;
15 18

16 19
import org.jfw.util.codec.Base64;
20
import org.jfw.util.exception.JfwBaseException;
17 21
import org.jfw.apt.annotation.Autowrie;
18 22
import org.jfw.apt.web.annotation.Path;
19 23
import org.jfw.apt.web.annotation.operate.Delete;
@ -22,6 +26,7 @@ import org.jfw.apt.web.annotation.operate.Post;
22 26
import org.jfw.apt.web.annotation.operate.Put;
23 27
import org.jfw.apt.web.annotation.param.JdbcConn;
24 28
import org.jfw.apt.web.annotation.param.PathVar;
29
import org.jfw.util.JpgUtil;
25 30
import org.jfw.util.StringUtil;
26 31
import org.jfw.util.io.IoUtil;
27 32

@ -33,6 +38,7 @@ import com.ekexiu.portal.util.PictureRotating;
33 38
public class ImagesService {
34 39
	private File tmpPath;
35 40
	private File resourcePath;
41
	private int resImageMaxLen = 240;
36 42
	@Autowrie
37 43
	private ImageDao imageDao;
38 44
	
@ -52,6 +58,14 @@ public class ImagesService {
52 58
		this.resourcePath = resourcePath;
53 59
	}
54 60

61
	public int getResImageMaxLen() {
62
		return resImageMaxLen;
63
	}
64

65
	public void setResImageMaxLen(int resImageMaxLen) {
66
		this.resImageMaxLen = resImageMaxLen;
67
	}
68

55 69
	public ImageDao getImageDao() {
56 70
		return imageDao;
57 71
	}
@ -60,15 +74,50 @@ public class ImagesService {
60 74
		this.imageDao = imageDao;
61 75
	}
62 76
	
63
	@Post
64
	@Path("/resourceImg")
65
	public String insert(@JdbcConn(true) Connection con, String resourceId, String fn) throws SQLException, IOException {
66
		InputStream in = new FileInputStream(new File(this.tmpPath, fn));
77
	/**
78
	 * 图片按比例缩放
79
	 * @param src 原图片
80
	 * @param maxLen 最长一边的长度
81
	 * @return 返回缩放后的图片
82
	 * @throws IOException
83
	 */
84
	public static byte[] resImage(byte[] src, int maxLen) throws IOException{
85
		ByteArrayInputStream in = new ByteArrayInputStream(src);
86
		ByteArrayOutputStream out = new ByteArrayOutputStream();
87
		BufferedImage bi = ImageIO.read(in);
88
	    double max = bi.getWidth()>bi.getHeight()?bi.getWidth():bi.getHeight();
89
	    double scaling = maxLen/max;
90
	    AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(scaling,scaling), null);
91
		java.awt.Image Itemp = ato.filter(bi, null);
92
		ImageIO.write((BufferedImage) Itemp, "jpg", out);
93
		out.flush();
94
		return out.toByteArray();
95
	}
96
	
97
	private byte[] readTmpFile(String fn) throws JfwBaseException {
98
		File file = new File(this.tmpPath, fn);
99
		if (!file.exists())
100
			throw new JfwBaseException(90, "resource image not exists");
67 101
		try {
68
			IoUtil.copy(in, new FileOutputStream(new File(this.resourcePath, resourceId + ".jpg")), false, true);
69
		} finally {
70
			in.close();
102
			InputStream in = new FileInputStream(file);
103
			ByteArrayOutputStream out = new ByteArrayOutputStream();
104
			IoUtil.copy(in, out, true, true);
105
			return out.toByteArray();
106
		} catch (IOException e) {
107
			throw new JfwBaseException(91, "read temp resource image error", e);
71 108
		}
109
	}
110
	
111
	@Post
112
	@Path("/resourceImg")
113
	public String insert(@JdbcConn(true) Connection con, String resourceId, String fn) 
114
			throws SQLException, IOException, JfwBaseException {
115
		byte[] src = this.readTmpFile(fn);
116
		src = JpgUtil.read(src);
117
		byte[] shareResImage = resImage(src, this.resImageMaxLen);
118
		IoUtil.saveStream(new FileOutputStream(new File(this.resourcePath, resourceId + ".jpg")), src, true);
119
		IoUtil.saveStream(new FileOutputStream(new File(this.resourcePath, resourceId + "_s.jpg")), shareResImage, true);
120
		
72 121
		Image image1 = this.imageDao.queryOne(con, resourceId);
73 122
		if(null == image1){
74 123
			Image image = new Image();
@ -90,10 +139,14 @@ public class ImagesService {
90 139
		Base64 bs64 = new Base64();
91 140
		byte[] bs = bs64.decode(base64.getBytes("UTF-8"));
92 141
		String imageSrc = this.resourcePath + "/" + resourceId + ".jpg";
142
		String resImage = this.resourcePath + "/" + resourceId + "_s.jpg";
93 143
		FileOutputStream fos = new FileOutputStream(imageSrc);
144
		FileOutputStream fos2 = new FileOutputStream(resImage);
94 145
		try {
95 146
			if(0 == angle){
147
				byte[] bytes = resImage(bs, this.resImageMaxLen);
96 148
				fos.write(bs);
149
				fos2.write(bytes);
97 150
			}else{
98 151
				java.awt.Image image = PictureRotating.bytesToBufferedImage(bs);
99 152
				BufferedImage des = PictureRotating.Rotate(image, angle);
@ -101,11 +154,15 @@ public class ImagesService {
101 154
				@SuppressWarnings("unused")
102 155
				boolean flag = ImageIO.write(des, "jpg", out);
103 156
				byte[] bytes = out.toByteArray();
157
				byte[] bs2 = resImage(bytes, this.resImageMaxLen);
104 158
				fos.write(bytes);
159
				fos2.write(bs2);
105 160
			}
106 161
			fos.flush();
162
			fos2.flush();
107 163
		} finally {
108 164
			fos.close();
165
			fos2.close();
109 166
		}
110 167
		Image image1 = this.imageDao.queryOne(con, resourceId);
111 168
		if(null == image1){

+ 17 - 31
src/main/java/com/ekexiu/portal/service/ResourceService.java

@ -16,6 +16,7 @@ import org.jfw.apt.web.annotation.param.JdbcConn;
16 16
import org.jfw.apt.web.annotation.param.PathVar;
17 17
import org.jfw.util.PageQueryResult;
18 18
import org.jfw.util.StringUtil;
19
import org.jfw.util.exception.JfwBaseException;
19 20

20 21
import com.ekexiu.portal.dao.ImageDao;
21 22
import com.ekexiu.portal.dao.OperationDao;
@ -101,49 +102,34 @@ public class ResourceService {
101 102

102 103
	@Post
103 104
	@Path
104
	public String insert(@JdbcConn(true) Connection con, Resource resource, @Nullable String fn) {
105
	public String insert(@JdbcConn(true) Connection con, Resource resource, @Nullable String fn) 
106
			throws JfwBaseException, SQLException, IOException {
105 107
		String resourceId = StringUtil.buildUUID();
106
		try {
107
			if(null != fn){
108
				this.imagesService.insert(con, resourceId, fn);
109
			}
110
			resource.setResourceId(resourceId);
111
			this.resourceDao.insert(con, resource);
112
		} catch (SQLException | IOException e) {
113
			try {
114
				con.rollback();
115
			} catch (SQLException e1) {
116
				e1.printStackTrace();
117
			}
118
			e.printStackTrace();
108
		if(null != fn){
109
			this.imagesService.insert(con, resourceId, fn);
119 110
		}
111
		resource.setResourceId(resourceId);
112
		this.resourceDao.insert(con, resource);
120 113
		return resourceId;
121 114
	}
122 115
	
123 116
	@Post
124 117
	@Path("/saveRes")
125
	public String insertRes(@JdbcConn(true) Connection con, Resource resource, @Nullable String base64, Integer angle) {
118
	public String insertRes(@JdbcConn(true) Connection con, Resource resource, @Nullable String base64, Integer angle) 
119
			throws SQLException, IOException {
126 120
		String resourceId = StringUtil.buildUUID();
127
		try {
128
			if(null != base64){
129
				this.imagesService.insertResourceImg(con, resourceId, base64, angle);
130
			}
131
			resource.setResourceId(resourceId);
132
			this.resourceDao.insert(con, resource);
133
		} catch (SQLException | IOException e) {
134
			try {
135
				con.rollback();
136
			} catch (SQLException e1) {
137
				e1.printStackTrace();
138
			}
139
			e.printStackTrace();
121
		if(null != base64){
122
			this.imagesService.insertResourceImg(con, resourceId, base64, angle);
140 123
		}
124
		resource.setResourceId(resourceId);
125
		this.resourceDao.insert(con, resource);
141 126
		return resourceId;
142 127
	}
143 128

144 129
	@Post
145 130
	@Path("/updateResource")
146
	public void update(@JdbcConn(true) Connection con, Resource resource, @Nullable String fn) throws SQLException, IOException {
131
	public void update(@JdbcConn(true) Connection con, Resource resource, @Nullable String fn) 
132
			throws SQLException, IOException, JfwBaseException {
147 133
		if(null != fn){
148 134
			this.imagesService.insert(con, resource.getResourceId(), fn);
149 135
		}
@ -254,7 +240,7 @@ public class ResourceService {
254 240
	@Path("/pqRes")
255 241
	public PageQueryResult<Resource> queryPageRes(@JdbcConn(false) Connection con, @Nullable String key, 
256 242
			@Nullable String subject, @Nullable String industry, @Nullable String province, @Nullable String address, 
257
			@Nullable Integer authType, @DefaultValue("10") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
243
			@DefaultValue("1") Integer authType, @DefaultValue("10") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
258 244
		if(key!=null) key="%"+key+"%";
259 245
		if(subject!=null) subject ="%"+subject+"%";
260 246
		if(industry!=null)industry="%"+industry+"%";
@ -274,7 +260,7 @@ public class ResourceService {
274 260
	@Get
275 261
	@Path("/pq")
276 262
	public PageQueryResult<Resource> queryPage(@JdbcConn(false) Connection con, @Nullable String key, 
277
			@Nullable String subject, @Nullable String industry, @Nullable String address,@Nullable Integer authType, 
263
			@Nullable String subject, @Nullable String industry, @Nullable String address,@DefaultValue("1") Integer authType, 
278 264
			@DefaultValue("10") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
279 265
		if(key!=null) key="%"+key+"%";
280 266
		if(subject!=null) subject ="%"+subject+"%";