根据上传的MultipartFile通过springboot转化为File类型并调⽤通
过F。。。
1 @PostMapping("uploadExcel")
2public ResponseObj uploadExcel(@RequestParam("excelFile") MultipartFile file,@RequestParam("companyId") String companyId,
3 @RequestParam("productId") String productId,HttpServletRequest request) throws Exception {
4 ResponseObj response = new ResponseObj();
5 response.setData(Defined.STATUS_SUCCESS);
6 response.setMessage("⽂件上传成功!");
7 ResponseObj resp = new ResponseObj();
8 resp.setData(Defined.STATUS_ERROR);
9 resp.setMessage("不是⽂件!");
10 AliYunFileSetting setting = new AliYunFileSetting();
11 setting.setAccessKeyId(aliConstants.accessKeyId);
12 setting.setAccessKeySecret(aliConstants.accessKeySecret);
13 setting.setBucketName(aliConstants.bucketName);
14 setting.dpoint);
15 AliyunFileManager manager = Instance(setting);
16if(file.isEmpty()){
17 response.setData(Defined.STATUS_ERROR);
18 response.setMessage("不是⽂件!");
19return response;
20 }
21 String fileName = OriginalFilename();
22long fileSize = Size();
23 File path = new URL("classpath:").getPath());
24if(!ists()) path = new File("");
25 File upload = new AbsolutePath(),"static/images/upload/");
26if(!ists()) upload.mkdirs();
27 File tempFile = new File(upload+"/"+fileName);
28if(!ParentFile().exists()){
29 ParentFile().mkdirs();//创建⽗级⽂件路径
30 ateNewFile();//创建⽂件
31 }
32 String relativePath =lFilePath;//相对路径
33 String dir = aliConstants.aliyunHostOuter+"/"+relativePath;//云端绝对路径
34 File dest = new File(dir);
springboot原理pdf35if(!ists()){ //判断⽂件⽬录是否存在
36 dest.mkdir();//
37 }
38try {
39 ansferTo(tempFile);
40 InputStream is = new FileInputStream(tempFile);
41 String suffix=fileName.substring(fileName.lastIndexOf("."));//获取原始⽂件后缀.xlxs(含点)
42 String newFileName = IdGen.uuid()+suffix;
43boolean result = manager.upload(is, relativePath, newFileName);//上传⽂件,并建⽴随机⽂件名。
44// boolean result = manager.upload(is, dir, fileName);//上传阿⾥云,固定⽂件名
45if(result){
46 response.setData(dir+"/"+newFileName);//返回新建⽂件名的绝对路径
47// 数据库存⼊相对路径
48 BatchRecord batchRecord = new BatchRecord();
49 batchRecord.setFileName(newFileName);
50 batchRecord.setFilePath(relativePath+"/"+newFileName);
51 batchRecord.setFileSize(fileSize);
52 batchRecord.setIsDelete((byte) 0);
53 batchRecord.setStatus((byte) 0);
54 batchRecord.setId(IdGen.uuid());
55 batchRecord.NowTimestamp());
56 batchRecord.NowTimestamp());
57 batchRecord.setCompanyId(companyId);
58 batchRecord.setProductId(productId);
59 Integer resultNum = deviceService.addBatchRecord(batchRecord);
60if(resultNum>0){
61return response;
62 }
63return resp;
64 }else{
65 resp.setMessage("⽂件上传失败!");
66return resp;
67 }
68 } catch (IllegalStateException e) {
69// TODO Auto-generated catch block
70 e.printStackTrace();
71 resp.setMessage("⽂件上传异常!");
72return resp;
73 } catch (IOException e) {
74// TODO Auto-generated catch block
75 e.printStackTrace();
76 resp.setMessage("⽂件上传异常!");
77return resp;
78 }
79 }
参考的博客blog.csdn/heylun/article/details/78732451
内容
springboot部署之后⽆法获取项⽬⽬录的问题:
之前看到⽹上有提问在开发⼀个springboot的项⽬时,在项⽬部署的时候遇到⼀个问题:就是我将项⽬导出为jar包,然后⽤java -jar 运⾏时,项⽬中⽂件上传的功能⽆法正常运⾏,其中获取到存放⽂件的⽬录的绝对路径的值为空,⽂件⽆法上传。
不清楚此⽹友具体是怎么实现的,通常我们可以通过如下⽅案解决:
//获取跟⽬录
File path = new URL("classpath:").getPath());
if(!ists()) path = new File("");
System.out.println("path:"+AbsolutePath());
//如果上传⽬录为/static/images/upload/,则可以如下获取:
File upload = new AbsolutePath(),"static/images/upload/");
if(!ists()) upload.mkdirs();
System.out.println("upload url:"+AbsolutePath());
//在开发测试模式时,得到的地址为:{项⽬跟⽬录}/target/static/images/upload/
//在打包成jar正式发布时,得到的地址为:{发布jar包⽬录}/static/images/upload/
另外使⽤以上代码需要注意,因为以jar包发布时,我们存储的路径是与jar包同级的static⽬录,因此我们需要在jar包⽬录的application.properties配置⽂件中设置静态资源路径,如下所⽰:
#设置静态资源路径,多个以逗号分隔
以jar包发布springboot项⽬时,默认会先使⽤jar包跟⽬录下的application.properties来作为项⽬配置⽂件。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论