小编SFA*_*FAH的帖子

Java:如何处理文件大小超过tomcat的异常

我正在处理 spring boot 项目,在该项目中我想处理最大文件大小超出异常(我只想上传那些大小小于 10MB 的文件,如果有人尝试上传大于 10 MB 的文件,它应该返回一条消息而不是例外),在互联网上搜索后,我尝试了所有可能的解决方案,但没有任何效果

任何人都可以告诉我这件事吗?

文件上传控制器.class

@ControllerAdvice
@Controller
public class FileUploadController extends ResponseEntityExceptionHandler{
@ExceptionHandler(MaxUploadSizeExceededException.class)
    @RequestMapping(method = RequestMethod.POST, value = "/uploadChildPhoto/{childId}", produces = "application/json")
    public @ResponseBody ResponseEntity<?> uploadChildPhoto(Authentication authentication,
            @PathVariable("childId") Long childId, @RequestParam("file") MultipartFile file,MaxUploadSizeExceededException exc) {
        try {
            if (!file.isEmpty()) {
                ChildPhoto createdPhoto = childService.createChildPhoto(file, childId);
                return ResponseEntity.ok(createdPhoto);
            } else {
                throw new RuntimeException(
                        "You failed to upload " + file.getOriginalFilename() + " because the file was empty");
            }
        } catch (MultipartException ex) {
            ex.printStackTrace(); …
Run Code Online (Sandbox Code Playgroud)

java spring tomcat exception-handling multipart

5
推荐指数
1
解决办法
2719
查看次数

JavaMail:电子邮件中的内嵌图像

我想在邮件正文中附加内嵌图像,为此我使用以下方法一切都很好,但电子邮件中附加了带有“noname”的附件,我不想在电子邮件中添加任何附件我只想在电子邮件中显示图像body ,以下是我的代码和邮件的快照

public void forgotPasswordMail(String email,String token)
        {
              String to = email;
              Properties props = new Properties();
              props.put("mail.smtp.auth", "true");
              props.put("mail.smtp.starttls.enable", "true");
              props.put("mail.smtp.host", host);
              props.put("mail.smtp.port", "587");
              Session session = Session.getInstance(props,
                 new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                       return new PasswordAuthentication(username, password);
               }
                 });
              try {
                  MimeMessage message = new MimeMessage(session);
                  message.setSubject("HTML  mail with images");
                  message.setFrom(new InternetAddress("me@sender.com"));
                  message.addRecipient(Message.RecipientType.TO,
                       new InternetAddress("you@receiver.com"));

                  // Mail Body
                  MimeMultipart multipart = new MimeMultipart("related");
                  BodyPart textPart = new MimeBodyPart();
                  String htmlText ="<img src=\"cid:image\"> " + 
                  "<html><head><style>h1 {background-color: #FFF100;padding: …
Run Code Online (Sandbox Code Playgroud)

html java jakarta-mail email-attachments inline-images

4
推荐指数
1
解决办法
7186
查看次数

Spring-Boot:400 Bad request 错误,即使参数存在

在下面的方法中有两个参数childFilefile,我通过邮递员运行此调用,但当我检查日志时它给出 400 Bad Request 错误,它说参数childFile不存在,我附上代码、错误和邮递员屏幕截图请告诉我什么是问题

邮递员呼叫

控制器

@Transactional
    @RequestMapping(method = RequestMethod.POST, value = "api/addchild",produces="application/json")
    public  ResponseEntity<?> child(Authentication authentication,
            @RequestParam(value="childFile") String childFile, @RequestParam(value="file") MultipartFile file) {

        ObjectMapper mapper = new ObjectMapper();
        Child child;
        try {
            child=mapper.readValue(childFile, Child.class);
            Child createdChild=childRepo.save(child);
            if(createdChild!=null)
            {
             childService.createChildPhoto(file, createdChild.getId());
            }
            return ResponseEntity.ok("child created");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Child can not be created , please check your headers and string body again.");   
    }
Run Code Online (Sandbox Code Playgroud)

日志

Content-Disposition: form-data; name] with value …
Run Code Online (Sandbox Code Playgroud)

java spring bad-request http-status-code-400 spring-boot

3
推荐指数
1
解决办法
5万
查看次数