springboot文件保存
控制器层reportscontroller,文件实体类FileInfo,json信息类Responemsg,持久层FileMapper
接收前端的JSON,然后进行解析,提取出base64字符串。把base64解码回图片文件,然后进行文件md5,最后本地持久化和存入数据库。
这里前端json数据
{
"base64": "用这个网址http://www.ofmonkey.com/encrypt/image64把一张图片转成base64编码,去掉数据的头部信息data:image/png;base64 ,留下数据的主体部分",
"length": 6,
"name": "fdfsadfas.jpg"
}
一开始try/catch的时候没有在catch中打印输出,所以报错了都不知道。
@RestController
@RequestMapping("/file")
public class reportscontroller {
@Value("${fileup}")
private String fileup;
@Value("${reportup}")
private String reportup;
@Value("${backgroud}")
private String backgroud;
@Value("${picture}")
private String picture;
@Autowired
FileMapper fileMapper;
@RequestMapping(value = "/upload", method = {RequestMethod.GET,RequestMethod.POST})
public String addPicture(HttpServletRequest request, @RequestBody String file){
Responemsg responemsg=new Responemsg();
FileInfo fileInfo = new FileInfo();
if (file.isEmpty()){
return new Gson().toJson(responemsg);
}
try {
JSONObject jSONObject = JSONObject.fromObject(file);
// 拆分原文件名
String base64 = jSONObject.getString("base64");
int length = jSONObject.getInt("length");
// 将base64转成文件
if (Strings.isEmpty(base64)){
return new Gson().toJson(new Responemsg());
}
// 判断文件是否一致
// if (base64.length() != length) {
// return new Gson().toJson(new Responemsg());
// }
String name = jSONObject.getString("name");
int i = name.indexOf(".");
fileInfo.setFname(name.substring(0, i));
fileInfo.setFext(name.substring(i));
// 系统生成文件名
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String format = formatter.format(new Date().getTime());
int num = (int)(Math.random() * 1000);
fileInfo.setSysname(format+num);
File file1 = new File(reportup);
if (!file1.exists()){
file1.mkdirs();
}else {
fileInfo.setFpath(reportup+fileInfo.getSysname()+fileInfo.getFext());
File file2 = new File(reportup+fileInfo.getSysname()+fileInfo.getFext());
// 将base64转成文件
byte[] bytes = Base64.getDecoder().decode(base64);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
bos.write(bytes);
// 文件md5
String md5 = DigestUtils.md5DigestAsHex(new FileInputStream(file2));
fileInfo.setFmd5(md5);
}
if (fileMapper.insertpicture(fileInfo) > 0){
responemsg.setCode("200");
responemsg.setMsg("上传成功");
}else {
return new Gson().toJson(responemsg);
}
}catch (Exception ee){
ee.printStackTrace();
}
return new Gson().toJson(responemsg);
}
}
public class Responemsg {
public String code="default";
public String msg="操作失败";
public String data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
@Mapper
@Repository
public interface FileMapper {
// 添加图片信息
@Insert("insert into fileinfo(fname, sysname, fext, fmd5, fpath)values(#{fname}, #{sysname}, #{fext}, #{fmd5}, #{fpath});")
@Options(useGeneratedKeys = true, keyProperty = "fid", keyColumn = "fid")
int insertpicture(FileInfo fileInfo);
// 查询图片信息列表
@Select("select * from fileinfo")
List<FieldInfo> getFileInfoList();
// 根据id查询图片细节
@Select("select * from fileinfo where fid=#{fid}")
FileInfo getFileDetail(@Param("fid") Long fid);
// 根据id删除指定图片
@Delete("delete from fileinfo where fid=#{fid}")
int deleteFileById(@Param("fid") Long fid);
}
public class FileInfo {
private Long fid;
private String fname;
private String sysname;
private String fext;
private String fmd5;
private String fpath;
public FileInfo() {
}
public FileInfo(Long fid, String fname, String sysname, String fext, String fmd5, String fpath) {
this.fid = fid;
this.fname = fname;
this.sysname = sysname;
this.fext = fext;
this.fmd5 = fmd5;
this.fpath = fpath;
}
public Long getFid() {
return fid;
}
public void setFid(Long fid) {
this.fid = fid;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getSysname() {
return sysname;
}
public void setSysname(String sysname) {
this.sysname = sysname;
}
public String getFext() {
return fext;
}
public void setFext(String fext) {
this.fext = fext;
}
public String getFmd5() {
return fmd5;
}
public void setFmd5(String fmd5) {
this.fmd5 = fmd5;
}
public String getFpath() {
return fpath;
}
public void setFpath(String fpath) {
this.fpath = fpath;
}
@Override
public String toString() {
return "FileInfo{" +
"fid=" + fid +
", fname='" + fname + '\'' +
", sysname='" + sysname + '\'' +
", fext='" + fext + '\'' +
", fmd5='" + fmd5 + '\'' +
", fpath='" + fpath + '\'' +
'}';
}
}
json所需要的依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<!--<version>2.8.5</version>-->
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>