Skip to content

Commit e7a10ff

Browse files
committed
修改web服务器无法引用本地jar包问题
添加webp图片压缩
1 parent ab76cf5 commit e7a10ff

File tree

7 files changed

+110
-55
lines changed

7 files changed

+110
-55
lines changed

springboot-dubbo-web/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@
203203
<!--添加自己的启动类路径!-->
204204
<configuration>
205205
<mainClass>com.lzq.web.WebApplication</mainClass>
206+
<!--应用本地jar包-->
207+
<includeSystemScope>true</includeSystemScope>
206208
</configuration>
207209
<executions>
208210
<execution>

springboot-dubbo-web/src/main/java/com/lzq/web/controller/ExampleController.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ public class ExampleController {
6363
@Value("${resources.route}")
6464
public String fileLocation;
6565

66-
@Value("${qiniuyun.url}")
67-
public String url;
6866

6967
/**
7068
* 创建一个实例
@@ -117,14 +115,12 @@ public Map<String, Object> CreateFile(Example example, Content exampleContent, S
117115
example.setFileName(Long.toString(time));
118116
//把文件信息插入到数据库中
119117
bol = exampleService.insert(example);
120-
log.info("获取exampleId:"+example);
121118
if (bol){
122-
bol = accountService.addWorks(example.getUsername());
123-
example.setExampleId(example.getExampleId());
119+
//更新用户的个人作品数
120+
accountService.addWorks(example.getUsername());
124121
//保存实例内容
125122
bol = ExampleUtils.SaveExampleContent(example, exampleContent, content, exampleService, contentService);
126123
}
127-
log.info(bol.toString());
128124
return ResultMapUtils.ResultMap(bol, 0, example.getExampleId());
129125
} catch (Exception e) {
130126
e.printStackTrace();

springboot-dubbo-web/src/main/java/com/lzq/web/controller/IndexController.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,19 @@ public Map<String, Object> giteeCallBack(HttpServletRequest request) throws IOEx
173173
@PostMapping("/sendPasswordEmail")
174174
@ApiOperation("发送修改密码链接到邮箱")
175175
public Map<String, Object> sendPasswordEmail(Account account) {
176+
log.info("我进入了发送链接邮箱地址");
176177
HashMap<String, String> map = new HashMap<>();
177178
//判断用户名是否不为空
178-
if (StringUtils.isNotBlank(account.getUsername())) {
179-
map.put("email", account.getEmail());
180-
String token = JWTUtils.getToken(map);
181-
Mail mail = new Mail();
182-
mail.setTo(account.getEmail());
183-
mail.setSubject("http://localhost:8080/resetPwd?=" + token);
184-
//发送修改密码链接
185-
boolean b = mailService.sendActiveMail(mail);
186-
//返回token令牌
187-
return ResultMapUtils.ResultMap(true, 0, null);
188-
} else {
189-
//用户名为空
190-
return ResultMapUtils.ResultMap(false, 0, null);
191-
}
179+
map.put("email", account.getEmail());
180+
String token = JWTUtils.getToken(map);
181+
Mail mail = new Mail();
182+
mail.setTo(account.getEmail());
183+
mail.setSubject("请点击以下链接进行密码修改");
184+
mail.setMailContent("http://localhost:8080/resetPwd?token=" + token);
185+
//发送修改密码链接
186+
boolean b = mailService.sendActiveMail(mail);
187+
//返回token令牌
188+
return ResultMapUtils.ResultMap(true, 0, null);
192189
}
193190

194191
/**

springboot-dubbo-web/src/main/java/com/lzq/web/utils/ExampleUtils.java

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.lzq.web.utils;
22

33

4+
import com.luciad.imageio.webp.WebPWriteParam;
45
import com.lzq.api.pojo.Content;
56
import com.lzq.api.pojo.Example;
67
import com.lzq.api.service.ContentService;
78
import com.lzq.api.service.ExampleService;
89
import com.qiniu.util.StringUtils;
10+
import lombok.extern.slf4j.Slf4j;
911
import org.openqa.selenium.Dimension;
1012
import org.openqa.selenium.OutputType;
1113
import org.openqa.selenium.TakesScreenshot;
@@ -14,6 +16,11 @@
1416
import org.springframework.beans.factory.annotation.Value;
1517
import org.springframework.stereotype.Component;
1618

19+
import javax.imageio.IIOImage;
20+
import javax.imageio.ImageIO;
21+
import javax.imageio.ImageWriter;
22+
import javax.imageio.stream.FileImageOutputStream;
23+
import java.awt.image.BufferedImage;
1724
import java.io.File;
1825
import java.io.FileNotFoundException;
1926
import java.io.FileOutputStream;
@@ -22,6 +29,7 @@
2229
import java.util.UUID;
2330
import java.util.concurrent.TimeUnit;
2431

32+
@Slf4j
2533
@Component
2634
public class ExampleUtils {
2735

@@ -33,8 +41,6 @@ public class ExampleUtils {
3341

3442
public static String BUCKET;
3543

36-
public static String URL;
37-
3844
private static final char[] _UU64 = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".toCharArray();
3945

4046
@Value("${resources.InitHtml}")
@@ -57,21 +63,16 @@ public void setBucket(String bucket) {
5763
ExampleUtils.BUCKET = bucket;
5864
}
5965

60-
@Value("${qiniuyun.url}")
61-
public void setUrl(String url) {
62-
ExampleUtils.URL = url;
63-
}
6466

6567
/**
6668
* 截图
6769
*
6870
* @param username 用户名
6971
* @param filename 文件名
70-
* @param imgname
7172
* @return
7273
* @throws IOException
7374
*/
74-
public static String screenshot(String username, String filename, String imgname) throws IOException {
75+
public static String screenshot(String username, String filename) throws IOException {
7576
//使用截屏工具进行截屏
7677
//启用chrome驱动
7778
//chrome驱动的位置
@@ -90,20 +91,29 @@ public static String screenshot(String username, String filename, String imgname
9091
broswer.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
9192
//打开url
9293
broswer.get("http://localhost:8090/" + username + "/" + filename + ".html");
93-
94-
byte[] bytes = ((TakesScreenshot) broswer).getScreenshotAs(OutputType.BYTES);
95-
//返回图片名
96-
String fileName = QiniuyunUtils.uploadFiles(bytes, imgname);
94+
//截图
95+
File screenshotAs = ((TakesScreenshot) broswer).getScreenshotAs(OutputType.FILE);
96+
//生成的webp文件
97+
File file = new File(FILE_LOCATION + "/" + username + "/" + username + ".webp");
98+
convertWebp(screenshotAs, file);
99+
//把生成的webp文件转换位byte数组
100+
//上传到七牛云
101+
String imgName = QiniuyunUtils.uploadFile(file);
97102
broswer.close();
98-
return fileName;
103+
//删除截图原始图片缓存
104+
screenshotAs.delete();
105+
boolean delete = file.delete();
106+
log.info(Boolean.toString(delete));
107+
return imgName;
99108

100109
}
101110

102111
/**
103112
* 保存实例
113+
*
104114
* @param example
105115
* @param exampleContent
106-
* @param content 编译后的内容
116+
* @param content 编译后的内容
107117
* @param exampleService
108118
* @param contentService
109119
* @return
@@ -114,7 +124,7 @@ public static Boolean SaveExampleContent(Example example, Content exampleContent
114124
//实例内容和实例进行绑定
115125
exampleContent.setExampleId(example.getExampleId());
116126
String file = FILE_LOCATION + example.getUsername() + "/" + example.getFileName() + ".html";
117-
System.out.println(file);
127+
log.info(file);
118128
FileOutputStream fos = new FileOutputStream(new File(file));
119129
String screenshot = null;
120130
Boolean bol = false;
@@ -123,23 +133,22 @@ public static Boolean SaveExampleContent(Example example, Content exampleContent
123133
fos.write(content.getBytes("GBK"));
124134
//第一次保存时生成图片
125135
if (StringUtils.isNullOrEmpty(example.getImg())) {
126-
//把当前时间戳设置为图片名称
127-
example.setImg(Long.toString(System.currentTimeMillis()));
128136
//截图后进行保存
129-
screenshot = ExampleUtils.screenshot(example.getUsername(), example.getFileName(), example.getImg());
137+
screenshot = ExampleUtils.screenshot(example.getUsername(), example.getFileName());
138+
//把当前时间戳设置为图片名称
139+
example.setImg(screenshot);
130140
} else {
131141
//先删除图片后上传新图片
132142
QiniuyunUtils.deleteFiles(example.getImg());
133-
example.setImg(Long.toString(System.currentTimeMillis()));
134143
//截图后进行保存
135-
screenshot = ExampleUtils.screenshot(example.getUsername(), example.getFileName(), example.getImg());
144+
screenshot = ExampleUtils.screenshot(example.getUsername(), example.getFileName());
145+
example.setImg(screenshot);
136146
}
137-
//修改图片地址
138-
example.setImg(URL + screenshot);
139147
//更新实例
140148
bol = exampleService.update(example);
141-
//修改实例内容 当表无该数据时插入数据
149+
//修改实例内容
142150
bol = contentService.updateContent(exampleContent);
151+
//当表无该数据时插入数据
143152
if (!bol) {
144153
//第一次保存时在表中添加实例内容
145154
bol = contentService.addContent(exampleContent);
@@ -153,8 +162,37 @@ public static Boolean SaveExampleContent(Example example, Content exampleContent
153162
}
154163
}
155164

165+
166+
/**
167+
* 转换webp
168+
*
169+
* @param oldfile 要转换的文件
170+
* @param newfile 生成的webp文件
171+
* @return
172+
* @throws IOException
173+
*/
174+
public static void convertWebp(File oldfile, File newfile) throws IOException {
175+
// Obtain an image to encode from somewhere
176+
BufferedImage image = ImageIO.read(oldfile);
177+
178+
// Obtain a WebP ImageWriter instance
179+
ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();
180+
181+
// Configure encoding parameters
182+
WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
183+
writeParam.setCompressionMode(WebPWriteParam.MODE_DEFAULT);
184+
185+
// Configure the output on the ImageWriter
186+
FileImageOutputStream fileImageOutputStream = new FileImageOutputStream(newfile);
187+
writer.setOutput(fileImageOutputStream);
188+
// Encode
189+
writer.write(null, new IIOImage(image, null, null), writeParam);
190+
fileImageOutputStream.close();
191+
}
192+
193+
156194
//生成22为uuid
157-
public static String getUUid(){
195+
public static String getUUid() {
158196
UUID uuid = UUID.randomUUID();
159197
int index = 0;
160198
char[] cs = new char[22];

springboot-dubbo-web/src/main/java/com/lzq/web/utils/QiniuyunUtils.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
import com.qiniu.storage.UploadManager;
1010
import com.qiniu.storage.model.DefaultPutRet;
1111
import com.qiniu.util.Auth;
12+
import lombok.extern.slf4j.Slf4j;
1213
import org.springframework.beans.factory.annotation.Value;
1314
import org.springframework.stereotype.Component;
1415

1516
import java.io.ByteArrayInputStream;
17+
import java.io.File;
1618

1719
/**
1820
* @author :LZQ
1921
* @description:TODO
2022
* @date :2021/8/26 10:53
2123
*/
24+
@Slf4j
2225
@Component
2326
public class QiniuyunUtils {
2427

@@ -42,19 +45,18 @@ public void setBucket(String bucket) {
4245
}
4346

4447
/**
45-
* 上传图片到七牛云
48+
* 上传图片的流到七牛云
4649
*
4750
* @param bytes
48-
* @param imgname 图片名称
4951
* @return
5052
*/
51-
public static String uploadFiles(byte[] bytes, String imgname) {
53+
public static String uploadFiles(byte[] bytes) {
5254
//构造一个带指定 Region 对象的配置类
5355
Configuration cfg = new Configuration(Region.huanan());
5456
//...其他参数参考类注释
5557
UploadManager uploadManager = new UploadManager(cfg);
5658
//默认不指定key的情况下,以文件内容的hash值作为文件名
57-
String key = imgname;
59+
String key = null;
5860
// byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
5961
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
6062
Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
@@ -63,11 +65,9 @@ public static String uploadFiles(byte[] bytes, String imgname) {
6365
DefaultPutRet putRet = null;
6466
try {
6567
Response response = uploadManager.put(byteInputStream, key, upToken, null, null);
66-
// new BucketManager(auth,cfg).prefetch(bucket, key);
6768
//解析上传成功的结果
6869
putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
69-
System.out.println(putRet.key);
70-
System.out.println(putRet.hash);
70+
log.info(putRet.key);
7171
} catch (QiniuException ex) {
7272
Response r = ex.response;
7373
System.err.println(r.toString());
@@ -80,6 +80,30 @@ public static String uploadFiles(byte[] bytes, String imgname) {
8080
return putRet.key;
8181
}
8282

83+
public static String uploadFile(File file) {
84+
//构造一个带指定 Region 对象的配置类
85+
Configuration cfg = new Configuration(Region.huanan());
86+
//...其他参数参考类注释
87+
UploadManager uploadManager = new UploadManager(cfg);
88+
//默认不指定key的情况下,以文件内容的hash值作为文件名
89+
String key = null;
90+
Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
91+
String upToken = auth.uploadToken(BUCKET);
92+
93+
try {
94+
Response response = uploadManager.put(file, key, upToken);
95+
//解析上传成功的结果
96+
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
97+
log.info(putRet.key);
98+
return putRet.key;
99+
} catch (QiniuException ex) {
100+
Response r = ex.response;
101+
System.err.println(r.toString());
102+
return null;
103+
}
104+
}
105+
106+
83107
/**
84108
* 删除七牛云图片
85109
*

springboot-dubbo-web/src/main/resources/application-dev.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ qiniuyun:
66
accessKey: Z_7eMJdtj_n4lrAdSs3zVuZ8rn4wZXu75b1gYJbC
77
secretKey: QIriVPlgNKoKdjU02q166-7IBPy3z9sQTMn5Ae7R
88
bucket: js-encoder
9-
url:
109
chorme:
1110
value: /usr/bin/chromedriver
1211

@@ -52,7 +51,7 @@ spring:
5251
password: wxr19980304
5352
#静态资源访问
5453
resources:
55-
static-locations: file:${User.data}
54+
static-locations: file:${resources.route}
5655
dubbo:
5756
application:
5857
name: dubbo_web

springboot-dubbo-web/src/main/resources/application-test.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ qiniuyun:
66
accessKey: fIf9nYz-wQo3HD1AlhQ5wrUrdjtygUPGe2dpuLlY
77
secretKey: ItVUX8sAaxsuj7kgmk3IZ0ip3BHljAx61N8m922C
88
bucket: lzqwxr
9-
url: firstbird.asia/
109

1110
chorme:
1211
value: F:/ChormeDriver/chromedriver.exe
@@ -41,7 +40,7 @@ spring:
4140
password: wxr19980304
4241
#静态资源访问
4342
resources:
44-
static-locations: file:${User.data}
43+
static-locations: file:${resources.route}
4544
dubbo:
4645
application:
4746
name: dubbo_web

0 commit comments

Comments
 (0)