上传文件spring boot和angular

jhiyze9q  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(303)

请问谁能帮我解决这个问题
org.apache.tomcat.util.http.fileupload.fileuploadexception:请求被拒绝,因为找不到多部分边界
文件控制器.java

package com.example.fileUploadApi.controller;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@RestController
@RequestMapping(path = "/fileUpload")
@CrossOrigin(origins = "http://localhost:4200")
@Produces(MediaType.APPLICATION_JSON)
public class FileController 
{
    @POST
    @Path("/upload")
    public  void uploadData(MultipartBody file) throws Exception {
        System.out.println(file);
    }
}

upload-file.component.html文件

<div>
   <input type="file" change="uploadFile($event)"  />
</div>

上传文件组件.ts

uploadFile(event) {
file = event.target.files[0]
  const formData = new FormData();
  const dali = {
       a: 'dali'
   };
  formData.append('file', file);
  formData.append('model', JSON.stringify(dali));
  this.fileUploadService.upload(formData).subscribe(
    rsp => {console.log(rsp.type);}
}
vktxenjb

vktxenjb1#

在您的spring boot项目中使用此代码这将100%起作用

@PostMapping("/process-contact")
    public String setContactForm(@Valid @ModelAttribute("contact") Contact contact,
            @RequestParam("profileImage") MultipartFile file, Principal principal, Model model, BindingResult rs) {

        if (rs.hasErrors()) {
            model.addAttribute("contact", contact);
            model.addAttribute("warning", rs.getFieldError());

            return "user/home";

        }

        User user = this.userRepositery.findUserByUserEmail(principal.getName());

        try {

            if (file.isEmpty()) {
                contact.setImage("1.png");
            } else {

                String saveImagePath = "UCNT" + new Date().getTime() + file.getOriginalFilename();

                File saveDirectory = new ClassPathResource("static/img/contact").getFile();

                Path path = Paths.get(saveDirectory.getAbsolutePath() + File.separator + saveImagePath);

                contact.setImage(saveImagePath);

                Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);

            }

            contact.setUser(user);

            user.getContacts().add(contact);

            this.userRepositery.save(user);
            model.addAttribute("user", user);

            model.addAttribute("message", "Contact has added successfully.");

        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
            model.addAttribute("warning", "Somthing went wrong try again.");

        }

        return "user/add-contact";
    }
tjjdgumg

tjjdgumg2#

在服务中将formdata作为'params'传递给您,并使用@requestparams在api中获取它。我在post调用中没有看到@requestparams。

相关问题