1.springmvc中实现文件上传到服务器
package com.oracle.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
/**
* greated with IntelliJ IDEA
* User: cn
* Date 2023 01 15 20:20
* Description:
* Version V1.0
*/
/**
* springboot使用文件上传
*
* 1.前端需要将表单中 enctype="application/x-www-form-urlencoded" 改为 enctype="multipart/form-data"
* 2.后端使用 MultipartFile接受文件
*/
@Api("上传文件工具类")
@RestController
public class FileUploadController {
@ApiOperation("接受前端文件的方法")
@PostMapping("/upload")
public String up(String nickName, MultipartFile photo, HttpServletRequest request) throws IOException {
System.out.println("用户的名字是:"+nickName);
System.out.println("文件的原始名称:"+photo.getOriginalFilename());
System.out.println("获取文件的类型是:"+photo.getContentType());
//获取服务器的路径
String path = request.getServletContext().getRealPath("/upload/");
System.out.println(path);
save(photo,path);
return "上传成功";
}
public void save(MultipartFile photo,String path) throws IOException {
File dir = new File(path);
if(!dir.exists()){
//不存在则创目录
dir.mkdir();
}
File file = new File(path + photo.getOriginalFilename());
//文件在此进行存储
photo.transferTo(file);
}
}
2.使用七牛云的oss对象存储文件,实现文件的储存
实现思路 图示
业务流程
客户端在上传资源到七牛云存储之前要先从业务服务器获取一个有效的上传凭证,因此需要先后和两个服务端打交道
如果有设置回调,则上传完成时七牛云存储会自动发起回调到指定的业务服务器。
使用七牛云的java sdk 实现
https://developer.qiniu.com/kodo/1239/java
1.前端发送请求获取token,并把令牌返回给前端
package com.oracle.util;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
public class GetQinNiuUtil {
public String getUpload_Token(){
String accessKey = "NzThbLJgsxwy0efa_ie23CoH5KxroQfwYKTnbQBP43123";
String secretKey = "aRgKt9QUOsNmJxgb0ho-cTsEEul_9JJYbAn_9SSf1231";
String bucket = "javamvc2023131313";
String key = "233";
Auth auth = Auth.create(accessKey, secretKey);
StringMap putPolicy = new StringMap();
// 数据处理完成结果通知地址
putPolicy.put("callbackUrl", "http://trfz4j.natappfree.cc/book/callback");
putPolicy.put("callbackBody", "key=$(key)&hash=$(etag)&bucket=$(bucket)&fsize=$(fsize)&userid=88888");
long expireSeconds = 3600;
String upToken = auth.uploadToken(bucket, null, expireSeconds, putPolicy);
System.out.println(upToken);
return upToken;
}
}
2.前端提交文件时携带令牌,和要上传的文件,提交到七牛云
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<form method="post" action="http://up-z2.qiniup.com/"
enctype="multipart/form-data">
<input name="token" type="hidden" value="JTnF8dgmcGcl2szrf9V4jV9clEQdwIy63U1Nndtq:YiA7HLD35E3997dNAJ_r-r9PEiQ=:eyJzY29wZSI6ImphdmEyMDIzIiwiY2FsbGJhY2tVcmwiOiJodHRwOi8va2l6c3VrLm5hdGFwcGZyZWUuY2MvY2FsbGJhY2siLCJkZWFkbGluZSI6MTY3NTIyNTk2MCwiY2FsbGJhY2tCb2R5Ijoia2V5XHUwMDNkJChrZXkpXHUwMDI2aGFzaFx1MDAzZCQoZXRhZylcdTAwMjZidWNrZXRcdTAwM2QkKGJ1Y2tldClcdTAwMjZmc2l6ZVx1MDAzZCQoZnNpemUpIn0=">
<input name="file" type="file" />
<input type="submit" value="上传文件" />
</form>
</body>
</html>
3.文件提交到七牛云后,数据处理完成结果通知我们的服务器,然后就可以取得文件地址,以及参数
@RequestMapping("/callback")
public void callback(String key,String hash,String bucket,String fsize,String userid){
System.out.println("回调的参数是----------");
System.out.println(key);
System.out.println(hash);
System.out.println(bucket);
System.out.println(fsize);
System.out.println(userid);
//取得文件的地址后和参数
直接插入数据库即可
}
System.out.println(fsize);
System.out.println(userid);
//取得文件的地址后和参数
直接插入数据库即可
}