1、span标签超出
在span标签里面填写内容时,如果输入的字数超出了span标签的宽度,若不进行处理,那么将会使span标签拉长,此时需要在span标签里面输入如下css样式:
word-wrap: break-word;word-break: break-all; 即可解决
2、文件上传
@Controller
public class UploadDemo {
/*上传路径*/
@Value("${web.file.path}")
private String filePath;
@RequestMapping("upload")
@ResponseBody
public ResultCode upload(@RequestParam("head.img") MultipartFile file, HttpServletRequest request){
try {
if (!file.isEmpty()){
/*获取名称*/
String name = request.getParameter("name");
/*获取系统上传的名称*/
String fileName = file.getOriginalFilename();
/*获取文件后缀名*/
String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);
/*获取文件新名称*/
String newFileName = name + UUID.randomUUID()+"."+suffixName;
String uploadPath = filePath+"/";
File file1 = new File(uploadPath);
/*判断路径是否存在*/
if (!file1.isDirectory()){
file1.mkdirs();
}
/**/
File file2 = new File(uploadPath+newFileName);
/*判断上传的文件是否存在*/
if (file2.exists()){
Files.delete(file1.toPath());
}
file.transferTo(file2);
return new ResultCode(1,"上传成功");
}
} catch (IOException e) {
return new ResultCode(-1,"上传失败");
}
return new ResultCode(-1,"上传失败");
}
}
@Value("${web.file.path}")
private String filePath;此为文件上传的路径,在appliaction.properties配置文件中进行书写,但是需要注意在配置文件中写文件上传时,不能出现任何中文字符,其次需要确保自己的文件上传路径是没问题的
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory factory = new MultipartConfigFactory();
//设置单个文件的上传大小
factory.setMaxFileSize(DataSize.parse("102400KB"));
//设置总数据量大小
factory.setMaxRequestSize(DataSize.parse("1024000KB"));
return factory.createMultipartConfig();
}
其次,在文件上传中,文件的大小是有限制的,当上传的文件超过上传的限制时,则会报错,因此需要在启动类中添加上述代码,设置单个文件上传的大小和上传总数据量大小
3、自定义异常
①创建自定义异常实体类(MyException,并创建字段,code,message)
package com.example.example.dao;
/*自定义异常实体类*/
public class ExceptionDao extends RuntimeException {
/*状态码*/
private int code;
/*错误信息*/
private String message;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public ExceptionDao(int code, String message) {
this.code = code;
this.message = message;
}
}
②创建异常处理类(ExceptionHander)
@ControllerAdvice
public class MyExceptionHander {
private Logger log = LoggerFactory.getLogger(getClass());
/*捕获全局异常*/
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Object getAllExecption(Exception e, HttpServletRequest request){
Map<String,Object> map = new HashMap<>();
map.put("code",100);
map.put("message","出现异常");
map.put("url",request.getRequestURL());
return map;
}
/*捕获自定义异常*/
/*跳转到界面*/
@ExceptionHandler(value = ExceptionDao.class)
public Object getMyException(ExceptionDao e, HttpServletRequest request){
ModelAndView mvc = new ModelAndView();
mvc.setViewName("error.html");
mvc.addObject("msg",e.getMessage());
return mvc;
}
捕获全局异常和自定义异常,全局异常出现时使用json,自定义异常出现时使用页面(error.html)
③创建异常类的controller
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("myException")
public class MyExceptionController {
@RequestMapping("toMy")
@ResponseBody
public Map<String,Object> toMyException(){
Map<String,Object> map = new HashMap<>();
map.put("name","zs");
map.put("age",14);
int i = 1/0;
return map;
}
@RequestMapping("myexce")
public Object testMyException() {
throw new ExceptionDao(500, "my ext异常");
}
根据不同的路径显示不同的结果