第一次上传文件到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:接口地址
posted @ 2019-08-29 20:03  止心静坐  阅读(600)  评论(0编辑  收藏  举报