最近做了個小網站,就是用tinyce富文本編輯器,https://www.511easy.com/ 保持字體排版和圖片
發現博客園的圖片,一天之后就無法顯示
就想着自己做一個圖片服務器,上傳圖片到指定的目錄,然后返回新的訪問地址
上傳圖片的接口很容易,很快就寫好了
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@Service
public class ImageServiceImpl implements ImageService {
@Value("${pic-path}")
private String save_path;
@Override
public String uploadImg(MultipartFile file) throws IOException {
String path = null;// 文件路徑
double fileSize = file.getSize();
System.out.println("文件的大小是" + fileSize);
byte[] sizebyte = new byte[0];
try {
sizebyte = file.getBytes();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件的byte大小是" + sizebyte.toString());
if (file != null) {// 判斷上傳的文件是否為空
String type = null;// 文件類型
String fileName = file.getOriginalFilename();// 文件原名稱
String newName= UUID.randomUUID().toString();
System.out.println("上傳的文件原名稱:" + fileName);
// 判斷文件類型
type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
if (type != null) {// 判斷文件類型是否為空
if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) {
// 項目在容器中實際發布運行的根路徑
//String realPath = request.getSession().getServletContext().getRealPath("/");
// 自定義的文件名稱
//String trueFileName = String.valueOf(System.currentTimeMillis()) + "." + type;
// 設置存放圖片文件的路徑
path = save_path + newName+"."+type;
System.out.println("存放圖片文件的路徑:" + path);
// 轉存文件到指定的路徑
file.transferTo(new File(path));
System.out.println("文件成功上傳到指定目錄下");
String imagePath = path;
FileSystemResource avatar = new FileSystemResource(imagePath);
return avatar.toString();
//return "文件成功上傳到指定目錄下";
}
} else {
System.out.println("不是我們想要的文件類型,請按要求重新上傳");
return "不是我們想要的文件類型,請按要求重新上傳";
}
} else {
System.out.println("文件類型為空");
return "文件類型為空";
}
return "已經成功上傳到指定目錄";
}
}
讀取圖片並顯示,就非常累,想用InputStream流讀取,之后在用html接收,發現這樣真麻煩
問過高人,高人指點用tomcat做圖片服務器啊,指定訪問的path就ok
自己試了一下,完全ok
下面是步驟:
1. 本地總要安裝jdk,配置java_home
2. 下載tomcat,配置conf目錄下的server.xml
<Context path="/img" docBase="C:/pic" reloadable="false"></Context> 就是我指定的訪問的路徑,訪問ip:port后的img目錄下的圖片名稱
C:/pic是指定服務器的目錄
這邊的端口,我用了8051
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8051" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8444" />
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="/img" docBase="C:/pic" reloadable="false"></Context>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
3. 啟動tomcat
我是在windows下啟動的,進入bin目錄下,點擊startup.bat即可
遇到的問題,我在本地OK,在遠程windows上一樣啟動失敗,發現遠程window上點擊startup.bat總是閃退,也看不到日志
之后首先要知道是什么原因,閃退,用cmd命令行啟動startup.bat,報錯java_home不存在,其實該機器安裝了JDK,只是現在很多應用能直接用,就沒有配置路徑
然后系統變量里面配置JDK,之后再次運行就成功了
http://118.25.182.23:8051/img/1.jpg
4. 上述的啟動,不能關閉啟動的tomcat,並且電腦關機不能自動開啟tomcat服務
這個時候用bin目錄下的執行 service.bat install
看到提示Tomcat9 has been installed
這個時候開啟服務即可后台運行 net start Tomcat9
重啟自動運行,到services.msc中找到對應的Apache Tomcat,將服務設置為自動即可