span标签里面输入的内容过长以至于超出级文件上传的步骤及错误

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异常");

    }

根据不同的路径显示不同的结果
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值