当前位置:网站首页>如何快速搭建图片服务器[通俗易懂]
如何快速搭建图片服务器[通俗易懂]
2022-07-25 21:43:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
前言
分布式集群的项目, 正常一般的工程是把图片放在web项目的自身服务器的工程中,但在集群环境下,会出现找不到图片的情况。
代码参考: https://github.com/zyjcxc/taotao.git
比如:
解决办法:
linux做磁盘的映射,说能解决,但服务器多了也不好弄,所以可以再搭建一个图片服务器
图片服务器两个服务:
- http:可以使用nginx做静态资源服务器。也可以使用apache。推荐使用nginx,效率更高。也可以结合我之前的mongo文件服务器,将文件url存到Mongo中。
- ftp服务:使用linux做服务器,在linux中有个ftp组件vsftpd。
步骤
服务器需求
Linux:CentOS6.4
Nginx:1.8.0
Vsftpd:需要在线安装。Nginx安装
参考 Nginx安装指南.
Vsftpd安装
参考 Vsftpd安装指南.
图片服务器的测试
安装好环境后,可以用ftp客户端(FileZilla)测试是否能上传下载文件。
ftp上传图片后, 用浏览器直接访问图片路径,测试是否有图片显示。
java代码实现上传
使用apache的FTPClient工具访问ftp服务器。需要在pom文件中添加依赖:
<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、连接ftp服务器
FTPClient ftpClient = new FTPClient();
ftpClient.connect("192.168.1.133", 21);
//2、登录ftp服务器
ftpClient.login("ftpuser", "ftpuser");
//3、读取本地文件
FileInputStream inputStream = new FileInputStream(new File("D:\\Documents\\Pictures\\images\\2010062119283578.jpg"));
//4、上传文件
//1)指定上传目录
ftpClient.changeWorkingDirectory("/home/ftpuser/www/images");
//2)指定文件类型
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//第一个参数:文件在远程服务器的名称
//第二个参数:文件流
ftpClient.storeFile("hello.jpg", inputStream);
//5、退出登录
ftpClient.logout();
}
}后续可以根据需求,将其封装工具类,写service, contorller即可, 截取部分代码
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", "文件上传失败");
return map;
}
map.put("error", 0);
map.put("url", IMAGE_BASE_URL + "/" + imagePath + "/" + newName);
} catch (IOException e) {
map.put("error", 1);
map.put("message", "文件上传异常");
e.printStackTrace();
return map;
}
return map;
}注意
springmvc框架,在配置文件中要加入文件上传解析器
<!-- 定义文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设定默认编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127913.html原文链接:https://javaforall.cn
边栏推荐
- GPON introduction and Huawei OLT gateway registration and configuration process
- [interview: concurrent Part 24: multithreading: comprehensive exercise] sequence control
- Fastjson deserialization vulnerability utilization analysis collection
- Mysql8.0 MHA to achieve high availability "MHA"
- Airtest solves the problem that a password needs to be entered in the process of "automatic packaging" (the same applies to random bullet frame processing)
- Apache Shenyu admin authentication bypass vulnerability (cve-2021-37580) analysis and protection measures
- 再次来光顾
- Share | intelligent fire emergency management platform solution (PDF attached)
- 【面试:并发篇23:多线程:join】join再理解
- QT | learn about QT creator by creating a simple project
猜你喜欢

GPON介绍及华为OLT网关注册配置流程

When facing complex problems, systematic thinking helps you understand the essence of the problem
![[leetcode ladder] linked list · 021 merge two ordered linked lists](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[leetcode ladder] linked list · 021 merge two ordered linked lists

立创EDA——器件的创建01-电阻(二)

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

Job interviews are always a second kill? After reading the seckill system notes secretly stored by JD T8, I have given my knees

【leetcode天梯】链表 · 021 合并两个有序链表

Web3 entrepreneurship has all the elements of explosive growth of innovation

FAW red flag "King fried" is listed, which is safe and comfortable

【面试:并发篇25:多线程:volatile】可见性
随机推荐
All non isomorphic subgraphs of a directed complete graph of order 3 (number of different hook graphs)
Shopify sellers: share some tips for social media marketing!
[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决
如何用 Redis 实现分布式锁的?
【Redis底层解析】字符串类型
Interviewer of large factory: how to quickly query a table with tens of millions of data?
How to copy all files in one folder to another folder
Performance debugging -- chrome performance
人脸与关键点检测:YOLO5Face实战
Idea resolves the prompt of profile properties disappear
Naming rules for BSP of Quanzhi chip
[ManageEngine] value brought by Siem to enterprises
分享|智慧消防应急管理平台解决方案(附PDF)
At present, flynk CDC does not support mysql5.5. If you change the source code and release this restriction, there will be a lot of data problems?
[interview: concurrent Part 24: multithreading: comprehensive exercise] sequence control
LeetCode_ 93_ Restore IP address
How will Web3 change the future of people?
开源协议是否具有法律效力?
Create EDA - why should I learn EDA
Talk about what's going on with C # backstage GC?