第一次上传文件到linux服务器

 1    /**
 2      * 上传文件
 3      *
 4      * @return ResultModel
 5      * @param mf 文件
 6      * @param type 1:goods,2:goods-parameter,3:goods-file,4:goods-review
 7      * @author zh
 8      * @date 2019-08-23
 9      **/
10     @ApiOperation("上传文件")
11     @PostMapping("/uploadFile")
12     public ResultModel upload(MultipartFile mf,Integer type) throws IOException {
13 
14         try {
15             String imageType = null;
16             if (type == 1) {
17                 imageType = "goods";
18             }
19             if (type == 2) {
20                 imageType = "goods-parameter";
21             }
22             if (type == 3) {
23                 imageType = "goods-file";
24             }
25             if (type == 4) {
26                 imageType = "goods-review";
27             }
28             String imageUrl = PropertiesUtils.getInstace("config/webService.properties").getProperty("imageUrl");
29             String url = imageUrl + File.separator + imageType;
30             File file = new File(url);
31             if (!file.exists()) {
32                 file.mkdirs();// 创建文件根目录
33             }
34             String fileName = mf.getOriginalFilename();
35             String savePath = file.getPath() + File.separator + fileName;
36             File savefile = new File(savePath);
37             FileCopyUtils.copy(mf.getBytes(), savefile);
38             String imageUrlData = PropertiesUtils.getInstace("config/webService.properties").getProperty("imageUrlData");
39             String dbpath = imageUrlData + File.separator + imageType + File.separator + fileName;
40             if (dbpath.contains("\\")) {
41                 dbpath = dbpath.replace("\\", "/");
42             }
43             return ResultUtil.success(dbpath, "上传成功");
44         } catch (MaxUploadSizeExceededException e) {
45             throw new CcodException("上传文件应小于30MB");
46         } catch (IOException e) {
47             e.printStackTrace();
48             throw new CcodException("IO异常,请稍后重试");
49         } catch (Exception e) {
50             throw new CcodException("上传文件失败");
51         }
52     }

配置文件中相关内容

1 #服务器存放图片地址
2 imageUrl=/home/apache-tomcat-8.5.40/webapps/image
3 #数据库存放图片地址
4 imageUrlData=http://112.124.XXX.XX:8082/image

前端接口拼接

 http://112.124.XXX.XX:8082/hardware_webservice/back/upload/uploadFile
注:hardware_webservice:项目名
       back:controller下包名
       upload/uploadFile:接口地址

转载于:https://www.cnblogs.com/04241202-nan/p/11431720.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值