Pārlūkot izejas kodu

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

zzy.zhiyuan.foxmail 8 gadi atpakaļ
vecāks
revīzija
23f8e1e246

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

1
package com.ekexiu.portal.service;
1
package com.ekexiu.portal.service;
2

2

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

18

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

32

33
public class ImagesService {
38
public class ImagesService {
34
	private File tmpPath;
39
	private File tmpPath;
35
	private File resourcePath;
40
	private File resourcePath;
41
	private int resImageMaxLen = 240;
36
	@Autowrie
42
	@Autowrie
37
	private ImageDao imageDao;
43
	private ImageDao imageDao;
38
	
44
	
52
		this.resourcePath = resourcePath;
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
	public ImageDao getImageDao() {
69
	public ImageDao getImageDao() {
56
		return imageDao;
70
		return imageDao;
57
	}
71
	}
60
		this.imageDao = imageDao;
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
		try {
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
		Image image1 = this.imageDao.queryOne(con, resourceId);
121
		Image image1 = this.imageDao.queryOne(con, resourceId);
73
		if(null == image1){
122
		if(null == image1){
74
			Image image = new Image();
123
			Image image = new Image();
90
		Base64 bs64 = new Base64();
139
		Base64 bs64 = new Base64();
91
		byte[] bs = bs64.decode(base64.getBytes("UTF-8"));
140
		byte[] bs = bs64.decode(base64.getBytes("UTF-8"));
92
		String imageSrc = this.resourcePath + "/" + resourceId + ".jpg";
141
		String imageSrc = this.resourcePath + "/" + resourceId + ".jpg";
142
		String resImage = this.resourcePath + "/" + resourceId + "_s.jpg";
93
		FileOutputStream fos = new FileOutputStream(imageSrc);
143
		FileOutputStream fos = new FileOutputStream(imageSrc);
144
		FileOutputStream fos2 = new FileOutputStream(resImage);
94
		try {
145
		try {
95
			if(0 == angle){
146
			if(0 == angle){
147
				byte[] bytes = resImage(bs, this.resImageMaxLen);
96
				fos.write(bs);
148
				fos.write(bs);
149
				fos2.write(bytes);
97
			}else{
150
			}else{
98
				java.awt.Image image = PictureRotating.bytesToBufferedImage(bs);
151
				java.awt.Image image = PictureRotating.bytesToBufferedImage(bs);
99
				BufferedImage des = PictureRotating.Rotate(image, angle);
152
				BufferedImage des = PictureRotating.Rotate(image, angle);
101
				@SuppressWarnings("unused")
154
				@SuppressWarnings("unused")
102
				boolean flag = ImageIO.write(des, "jpg", out);
155
				boolean flag = ImageIO.write(des, "jpg", out);
103
				byte[] bytes = out.toByteArray();
156
				byte[] bytes = out.toByteArray();
157
				byte[] bs2 = resImage(bytes, this.resImageMaxLen);
104
				fos.write(bytes);
158
				fos.write(bytes);
159
				fos2.write(bs2);
105
			}
160
			}
106
			fos.flush();
161
			fos.flush();
162
			fos2.flush();
107
		} finally {
163
		} finally {
108
			fos.close();
164
			fos.close();
165
			fos2.close();
109
		}
166
		}
110
		Image image1 = this.imageDao.queryOne(con, resourceId);
167
		Image image1 = this.imageDao.queryOne(con, resourceId);
111
		if(null == image1){
168
		if(null == image1){

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

16
import org.jfw.apt.web.annotation.param.PathVar;
16
import org.jfw.apt.web.annotation.param.PathVar;
17
import org.jfw.util.PageQueryResult;
17
import org.jfw.util.PageQueryResult;
18
import org.jfw.util.StringUtil;
18
import org.jfw.util.StringUtil;
19
import org.jfw.util.exception.JfwBaseException;
19

20

20
import com.ekexiu.portal.dao.ImageDao;
21
import com.ekexiu.portal.dao.ImageDao;
21
import com.ekexiu.portal.dao.OperationDao;
22
import com.ekexiu.portal.dao.OperationDao;
101

102

102
	@Post
103
	@Post
103
	@Path
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
		String resourceId = StringUtil.buildUUID();
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
		return resourceId;
113
		return resourceId;
121
	}
114
	}
122
	
115
	
123
	@Post
116
	@Post
124
	@Path("/saveRes")
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
		String resourceId = StringUtil.buildUUID();
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
		return resourceId;
126
		return resourceId;
142
	}
127
	}
143

128

144
	@Post
129
	@Post
145
	@Path("/updateResource")
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
		if(null != fn){
133
		if(null != fn){
148
			this.imagesService.insert(con, resource.getResourceId(), fn);
134
			this.imagesService.insert(con, resource.getResourceId(), fn);
149
		}
135
		}
254
	@Path("/pqRes")
240
	@Path("/pqRes")
255
	public PageQueryResult<Resource> queryPageRes(@JdbcConn(false) Connection con, @Nullable String key, 
241
	public PageQueryResult<Resource> queryPageRes(@JdbcConn(false) Connection con, @Nullable String key, 
256
			@Nullable String subject, @Nullable String industry, @Nullable String province, @Nullable String address, 
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
		if(key!=null) key="%"+key+"%";
244
		if(key!=null) key="%"+key+"%";
259
		if(subject!=null) subject ="%"+subject+"%";
245
		if(subject!=null) subject ="%"+subject+"%";
260
		if(industry!=null)industry="%"+industry+"%";
246
		if(industry!=null)industry="%"+industry+"%";
274
	@Get
260
	@Get
275
	@Path("/pq")
261
	@Path("/pq")
276
	public PageQueryResult<Resource> queryPage(@JdbcConn(false) Connection con, @Nullable String key, 
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
			@DefaultValue("10") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
264
			@DefaultValue("10") int pageSize, @DefaultValue("1") int pageNo) throws SQLException {
279
		if(key!=null) key="%"+key+"%";
265
		if(key!=null) key="%"+key+"%";
280
		if(subject!=null) subject ="%"+subject+"%";
266
		if(subject!=null) subject ="%"+subject+"%";