引入依赖pom.xml添加:
<!--文件上传相关-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.49</version>
</dependency>
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>261</version>
</dependency>
自定义配置:
file:
server:
host: x.x.x.x
port: 22
username: root
password: 12345
fileAddress: /home/file # 文件服务器的绝对路径(linux)
fileVisitPath: http://ip:port/file/Icon # 文件访问路径
config包下创建FileServerProperties
package acw.com.sys.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "file.server")
public class FileServerProperties {
private String host;
private Integer port;
private String username;
private String password;
private String fileAddress;
private String fileVisitPath;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFileAddress() {
return fileAddress;
}
public void setFileAddress(String fileAddress) {
this.fileAddress = fileAddress;
}
public String getFileVisitPath() {
return fileVisitPath;
}
public void setFileVisitPath(String fileVisitPath) {
this.fileVisitPath = fileVisitPath;
}
}
文件上传接口(controller包下)类上加@EnableConfigurationProperties(FileServerProperties.class);
然后注入@Autowired
private FileServerProperties fileServerProperties;:
/**
* @Description //文件上传
**/
@PostMapping("/upload")
@ResponseBody
public Result fileUpload(@RequestParam("file") MultipartFile file) throws IOException {
if (file.isEmpty()) {
return new Result(false, "文件上传失败, 请选择文件");
}
System.out.println("fileName: " + file.getOriginalFilename());
try {
String originalFilename = file.getOriginalFilename(); //23hdsfjhsd_sdjf_dsf.jpg
int index = originalFilename.lastIndexOf(".");
String substring = originalFilename.substring(index); // .jpg
String newFileName = UUID.randomUUID().toString() + substring; // 99b7d09a-4719-4f5b-bc7b-8e2d4c4dbabb.jpg
String fileAddr = fileServerProperties.getFileVisitPath() + "/" +newFileName; //得到完整的文件访问路径,例:http://ascenway.imwork.net:29208/zhonghe/file/Icon/99b7d09a-4719-4f5b-bc7b-8e2d4c4dbabb.jpg
InputStream input = file.getInputStream(); //流的方式上传
System.out.println("文件访问的路径:"+fileAddr);
boolean result = FtpUtil.uploadFile(fileServerProperties.getHost(), fileServerProperties.getPort(), fileServerProperties.getUsername(), fileServerProperties.getPassword(), fileServerProperties.getFileAddress(), newFileName, input);
if (result) {
return new Result(true, "文件上传成功", fileAddr);
} else {
return new Result(false, "文件上传失败", "未知错误");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("异常------");
}
return new Result(false, "网络原因");
}
到这里文件就可以正常上传了
访问:
nginx配置:
server {
listen 8088;
server_name localhost;
location /file/Icon {
#只需修改下面图片绝对路径位置
alias /home/file/;
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
就可以通过上传文件后后台传来的路径进行访问了,例如:http://ip:port/file/Icon/1212121.jpg
下载:
<a href="http://ip:port/file/Icon/1212121.jpg" download>