当前位置:网站首页>How to quickly build a picture server [easy to understand]
How to quickly build a picture server [easy to understand]
2022-07-25 21:50:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Preface
Distributed cluster project , The normal general project is to put pictures on web In the project of the project's own server , But in the cluster environment , The picture cannot be found .
Code reference : https://github.com/zyjcxc/taotao.git
such as :
terms of settlement :
linux Do disk mapping , Said it could be solved , But it's hard to deal with too many servers , So you can build another image server
The image server has two services :
- http: have access to nginx Be a static resource server . You can also use apache. Recommended nginx, More efficient . It can also be combined with my previous mongo File server , Will file url Deposit in Mongo in .
- ftp service : Use linux Make servers , stay linux There was a ftp Components vsftpd.
step
Server requirements
Linux:CentOS6.4
Nginx:1.8.0
Vsftpd: Need to be installed online .Nginx install
Reference resources Nginx Installation guide .
Vsftpd install
Reference resources Vsftpd Installation guide .
Test of image server
After installing the environment , It can be used ftp client (FileZilla) Test whether you can upload and download files .
ftp After uploading the picture , Use the browser to directly access the image path , Test whether there are pictures .
java Upload the code
Use apache Of FTPClient Tool access ftp The server . Need to be in pom Add dependencies to the file :
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>${commons-net.version}</version>
</dependency>public class FTPClientTest {
@Test
public void testFtp() throws Exception {
//1、 Connect ftp The server
FTPClient ftpClient = new FTPClient();
ftpClient.connect("192.168.1.133", 21);
//2、 Sign in ftp The server
ftpClient.login("ftpuser", "ftpuser");
//3、 Read local file
FileInputStream inputStream = new FileInputStream(new File("D:\\Documents\\Pictures\\images\\2010062119283578.jpg"));
//4、 Upload files
//1) Specify upload directory
ftpClient.changeWorkingDirectory("/home/ftpuser/www/images");
//2) Specify file type
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// The first parameter : The name of the file on the remote server
// The second parameter : File stream
ftpClient.storeFile("hello.jpg", inputStream);
//5、 Log out
ftpClient.logout();
}
}You can follow up according to your needs , Encapsulate tool classes , Write service, contorller that will do , Intercept part of the code
public Map uploadPicture(MultipartFile uploadFile) {
Map<String, Object> map = new HashMap<>(2);
try {
String oldName = uploadFile.getOriginalFilename();
String newName = IDUtils.genImageName();
newName += oldName.substring(oldName.lastIndexOf("."));
String imagePath = new DateTime
().toString("yyyy/MM/dd");
boolean result = FtpUtil.uploadFile(FTP_HOST, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_BASE_PATH, imagePath, newName, uploadFile.getInputStream());
if (!result) {
map.put("error", 1);
map.put("message", " File upload failed ");
return map;
}
map.put("error", 0);
map.put("url", IMAGE_BASE_URL + "/" + imagePath + "/" + newName);
} catch (IOException e) {
map.put("error", 1);
map.put("message", " File upload exception ");
e.printStackTrace();
return map;
}
return map;
}Be careful
springmvc frame , Add a file upload parser to the configuration file
<!-- Define file upload parser -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- Set the default encoding -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- Set the maximum number of file uploads 5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/127913.html Link to the original text :https://javaforall.cn
边栏推荐
- 再次来光顾
- [Flink] flick rocksdbliststate reports an error you cannot add null to a liststate
- [database] index
- [JS] the problem pointed by this
- ZigBee development board (nxpzigbee Development)
- C语言左值和右值说明[通俗易懂]
- Zero basic learning canoe panel (17) -- panel CAPL function
- 【测开方法论】测开平台pk心得-抉择
- 2022-07-18: what is the output of the following go language code? A:Groutine; B:Main; C:Goroutine; D:GoroutineMain。 package m
- 卸载npm和安装npm_使用`npm uninstall`卸载npm软件包「建议收藏」
猜你喜欢

Research: more than 70% of doctors are still prescribing unsafe antibiotic drugs

Jmeter---设置代理录制请求

新版Maixhub部署(V831与K210)

开源的RSS订阅器FreshRSS
![[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决](/img/0b/da67b5a361a2cdfaf81568d34cf5f7.png)
[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决

I'm also drunk. Eureka delayed registration and this pit!

Shopify sellers: share some tips for social media marketing!

How to choose sentinel vs. hystrix current limiting?

【leetcode天梯】链表 · 876 查找链表中间结点

Detailed explanation of Ag search tool parameters
随机推荐
[leetcode ladder] linked list · 876 find the middle node of the linked list
5、 Pinda general permission system__ PD tools XXS (anti cross site script attack)
信息安全建设原则指导
C#Socket
Research: more than 70% of doctors are still prescribing unsafe antibiotic drugs
Detailed explanation of several ideas for implementing timed tasks in PHP
Jmeter---设置代理录制请求
[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决
[database] conceptual design, logical design, relational database design theory
Excuse me, how to deal with repeated consumption of MySQL data
[redis underlying parsing] linked list type
2 lines of code to generate a solid desktop background
Why do independent sellers like to do e-mail marketing? The original conversion rate can be improved so much!
【Flink】FLink RocksDBListState 报错 You cannot add null to a ListState
How to use RS485 half duplex chip correctly
[database] index
golang : MVC之models
Byte side: can TCP and UDP use the same port?
In Oracle 19C version, logminer package continuous_ The outdated function of mine leads to CDC failure
Bitcoin.com:USDD代表了真正去中心化稳定币