前言
用户头像的上传与下载。
配置信息
file:
path: "/www/meeting"
picture:
pattern: .jpg
1、PictureUtil.java
@Component
public class PictureUtil {
@Value("${file.path}")
public String path;
@Value("${picture.pattern}")
private String filePattern;
@Autowired
private UUIDUtil uuidUtil;
public void handlePicture(String type, MultipartFile file, long uid)
throws IOException{
String originalName = file.getOriginalFilename();
if (originalName == null) {
throw new IllegalStateException("文件名不能为空");
}
if (!originalName.endsWith(filePattern)) {
throw new FileFormatException("不支持的文件格式");
}
String fileName = "" + uid + filePattern;
File dest = new File(path + '/' + type + '/' + fileName);
if(!dest.getParentFile().exists()){
dest.getParentFile().mkdir();
}
file.transferTo(dest);
}
public byte[] openPicture(String filename, String type)
throws FileNotFoundException {
File source = new File(path + '/' + type + '/' + filename);
if (source.exists() && source.isFile()) {
InputStream inputStream = null;
ByteArrayOutputStream outputStream = null;
try {
inputStream = new FileInputStream(source);
outputStream = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int read = -1;
while ((read = inputStream.read(buff)) != -1) {
outputStream.write(buff, 0, read);
}
if (outputStream.size() == 0) {
throw new FileNotFoundException("空文件");
}
return outputStream.toByteArray();
} catch (IOException exception) {
return null;
} finally {
handleClose(inputStream, outputStream);
}
} else {
throw new FileNotFoundException("文件不存在");
}
}
private void handleClose(InputStream inputStream, OutputStream outputStream) {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
2、其它
前几天重新写了User类,其中profile字段变为boolean类型,表示是否采用默认头像。 在文件的上传与下载中,除了用到了PictureUtil 类,其它相较于之前的功能没有什么区别,这里就不做展示了。 然后就是在main函数所在的类里面, 添加了MultipartConfigElement的bean,用于限制文件大小。根据网上的说法,这个bean要配置在main函数所在的类里面,具体原因我不太清楚。
3、全局异常
增加了全局异常处理,留一下记录,将来使用。
@ControllerAdvice
public class GlobalExceptionHandler {
@ResponseBody
@ExceptionHandler(value = {MissingServletRequestParameterException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String, Object> handleException(MissingServletRequestParameterException exception) {
Map<String, Object> map = new HashMap<>();
map.put("code", 400);
map.put("message", exception.getMessage());
return map;
}
@ResponseBody
@ExceptionHandler(value = {MissingRequestHeaderException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Map<String, Object> handleException(MissingRequestHeaderException exception) {
Map<String, Object> map = new HashMap<>();
map.put("code", 400);
map.put("message", exception.getMessage());
return map;
}
@ResponseBody
@ExceptionHandler(value = {UnAuthorizedException.class})
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public Map<String, Object> handleException(UnAuthorizedException exception) {
Map<String, Object> map = new HashMap<>();
map.put("code", 401);
map.put("message", exception.getMsg());
return map;
}
@ResponseBody
@ExceptionHandler(value = {NoHandlerFoundException.class})
@ResponseStatus(HttpStatus.NOT_FOUND)
public Map<String, Object> handleException(NoHandlerFoundException exception) {
Map<String, Object> map = new HashMap<>();
map.put("code", 404);
map.put("message", exception.getMessage());
return map;
}
@ResponseBody
@ExceptionHandler(value = {FileNotFoundException.class})
@ResponseStatus(HttpStatus.NOT_FOUND)
public Map<String, Object> handleException(FileNotFoundException exception) {
Map<String, Object> map = new HashMap<>();
map.put("code", 404);
map.put("message", exception.getMessage());
return map;
}
@ResponseBody
@ExceptionHandler(value = {HttpRequestMethodNotSupportedException.class})
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public Map<String, Object> handleException(HttpRequestMethodNotSupportedException exception) {
Map<String, Object> map = new HashMap<>();
map.put("code", 405);
map.put("message", exception.getMessage());
return map;
}
@ResponseBody
@ExceptionHandler(value = {MaxUploadSizeExceededException.class})
public Map<String, Object> handleException(MaxUploadSizeExceededException exception) {
Map<String, Object> map = new HashMap<>();
map.put("code", 500);
map.put("message", exception.getMessage());
return map;
}
@ResponseBody
@ExceptionHandler(value = {Exception.class})
public Map<String, Object> exceptionHandler(Exception exception) {
System.out.println(exception.getMessage());
System.out.println(exception.getClass());
Map<String, Object> map = new HashMap<>();
map.put("code", 500);
map.put("message", "服务器出现异常");
return map;
}
}
|