文件上传到远程服务器,访问、下载

引入依赖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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值