27 changed files with 397 additions and 95 deletions
@ -0,0 +1,43 @@ |
|||
package com.ccsens.ht.api; |
|||
|
|||
import cn.hutool.core.date.DateUtil; |
|||
import com.ccsens.ht.annotation.MustLogin; |
|||
import com.ccsens.ht.bean.dto.FileDto; |
|||
import com.ccsens.ht.bean.dto.QueryDto; |
|||
import com.ccsens.ht.bean.dto.QuestionDto; |
|||
import com.ccsens.ht.bean.vo.FileVo; |
|||
import com.ccsens.ht.bean.vo.QuestionVo; |
|||
import com.ccsens.util.Base64FileUtil; |
|||
import com.ccsens.util.JsonResponse; |
|||
import com.ccsens.util.PropUtil; |
|||
import io.swagger.annotations.*; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RequestMethod; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import javax.validation.Valid; |
|||
|
|||
/** |
|||
* @description: |
|||
* @author: wuHuiJuan |
|||
* @create: 2019/12/17 18:03 |
|||
*/ |
|||
@Slf4j |
|||
@Api(tags = "文件上传",value = "文件上传") |
|||
@RestController |
|||
public class FileController { |
|||
@MustLogin |
|||
@ApiOperation(value = "测评试题查询",notes = "测评试题查询") |
|||
@ApiImplicitParams({ |
|||
@ApiImplicitParam(name = "json", value = "测评试题查询信息", required = true) |
|||
}) |
|||
@RequestMapping(value="/saveFile", method = RequestMethod.POST) |
|||
public JsonResponse<FileVo.Save> saveFile(@RequestBody @ApiParam @Valid QueryDto<FileDto.Save> queryDto) throws Exception { |
|||
String path = Base64FileUtil.base64ToFile(queryDto.getParam().getBase64(), PropUtil.path, "/file/" + DateUtil.today() + "/"); |
|||
FileVo.Save save = new FileVo.Save(); |
|||
save.setPath(PropUtil.imgDomain + path); |
|||
return JsonResponse.newInstance().ok(save); |
|||
} |
|||
} |
@ -0,0 +1,24 @@ |
|||
package com.ccsens.ht.bean.dto; |
|||
|
|||
import io.swagger.annotations.ApiModel; |
|||
import io.swagger.annotations.ApiModelProperty; |
|||
import lombok.Data; |
|||
|
|||
import javax.validation.constraints.NotNull; |
|||
|
|||
/** |
|||
* @description: |
|||
* @author: wuHuiJuan |
|||
* @create: 2019/12/17 18:05 |
|||
*/ |
|||
public class FileDto { |
|||
|
|||
@ApiModel("FileDtoSave") |
|||
@Data |
|||
public static class Save{ |
|||
|
|||
@ApiModelProperty("文件base64字符串") |
|||
@NotNull(message = "文件内容不允许为空") |
|||
private String base64; |
|||
} |
|||
} |
@ -0,0 +1,19 @@ |
|||
package com.ccsens.ht.bean.vo; |
|||
|
|||
import io.swagger.annotations.ApiModel; |
|||
import io.swagger.annotations.ApiModelProperty; |
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @description: |
|||
* @author: wuHuiJuan |
|||
* @create: 2019/12/17 18:04 |
|||
*/ |
|||
public class FileVo { |
|||
@ApiModel("FileVoSave") |
|||
@Data |
|||
public static class Save{ |
|||
@ApiModelProperty("文件访问路径") |
|||
private String path; |
|||
} |
|||
} |
@ -0,0 +1,45 @@ |
|||
package com.ccsens.util; |
|||
|
|||
import cn.hutool.core.codec.Base64; |
|||
|
|||
import java.io.File; |
|||
import java.io.FileNotFoundException; |
|||
import java.io.FileOutputStream; |
|||
import java.util.Random; |
|||
|
|||
/** |
|||
* @description: |
|||
* @author: wuHuiJuan |
|||
* @create: 2019/12/17 17:48 |
|||
*/ |
|||
public class Base64FileUtil { |
|||
|
|||
/** |
|||
* 将base64字符串转换成文件 |
|||
* @param base64 |
|||
* @param path |
|||
* @param visitPath 访问路径(无文件名) |
|||
* @return 返回文件访问路径(无域名) |
|||
* @throws Exception |
|||
*/ |
|||
public static String base64ToFile(String base64, String path, String visitPath) throws Exception { |
|||
//获取后缀 data:image/png;base64,xxxxxx
|
|||
String suffix = base64.substring(base64.indexOf("/")+1, base64.indexOf(";")); |
|||
Random random = new Random(); |
|||
String randomStr = String.valueOf(System.currentTimeMillis()) + random.nextInt(100) + "." + suffix; |
|||
String fileName = visitPath + randomStr; |
|||
File file = new File(path + fileName); |
|||
if (!file.getParentFile().exists()) { |
|||
file.getParentFile().mkdirs(); |
|||
} |
|||
// 截取base64,后面的字符串
|
|||
String base64flag = "base64,"; |
|||
int index = base64.indexOf(base64flag); |
|||
String str = index < 0 ? base64 : base64.substring(index + base64flag.length()); |
|||
byte[] decode = Base64.decode(str); |
|||
FileOutputStream out = new FileOutputStream(file); |
|||
out.write(decode); |
|||
out.close(); |
|||
return fileName; |
|||
} |
|||
} |
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue