对于上下文,我正在创建一个文件上传/下载RESTful服务,并在Postman中进行测试,我得到了一个错误,我不应该这样做。
这是我的 * 文档实体 *
import jakarta.persistence.*;
import lombok.*;
@Entity
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name = "t_document")
public class Document extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "title")
private String title;
@Lob
@Column(name = "data", columnDefinition = "LONGBLOB")
private byte[] data;
@Column(name = "name")
private String name;
@Column(name = "file_type")
private String fileType;
@ManyToOne
@JoinColumn(name = "application_id")
private ApplicationEntity applicationEntity;
}
BaseEntity
只是实现了可序列化,仅此而已。
这些是我的Service和ServiceImpl类
import org.springframework.web.multipart.MultipartFile;
public interface DocumentService {
Document saveDoc(MultipartFile file, String title) throws Exception;
Document getDoc(int id);
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
@Service
public class DocumentServiceImpl implements DocumentService{
@Autowired
private DocumentRepository documentRepository;
@Override
public Document saveDoc(MultipartFile file, String title) throws Exception {
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
try{
if(fileName.contains("..")){
throw new Exception("Filename contains invalid path sequence "+fileName);
}
Document fileEntity = Document.builder()
.title(title)
.name(fileName)
.fileType(file.getContentType())
.data(file.getBytes())
.build();
return documentRepository.save(fileEntity);
}catch (Exception e){
throw new Exception("Could not save File:" +fileName);
}
}
@Override
public Document getDoc(int id) {
return documentRepository.findById(id).orElse(null);
}
}
这是我的控制器类
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
@Slf4j
@RestController
@RequestMapping("/recruiter")
public class RecruiterLandingPage {
@Autowired
private DocumentService documentService;
@GetMapping("/hello")
public ResponseEntity<ApiResponse<String>> Hello(){
return ResponseEntity.ok().body(ApiResponse.of("Recruiter's landing page"));
}
@PostMapping("/candidate/uploadDocument")
public DocumentResponse uploadFile(@RequestParam("file") MultipartFile file) throws Exception{
Document document = null;
document = documentService.saveDoc(file, "title");
String downloadUrl = ServletUriComponentsBuilder
.fromCurrentContextPath()
.path("/download/")
.path(Integer.toString(document.getId()))
.toUriString();
return new DocumentResponse(
document.getName(),
downloadUrl,
file.getSize()
);
}
@PostMapping("/candidate/name")
public String returnName(@RequestParam("name") String name) throws Exception {
return name;
}
}
当我在 Postman 上运行时,我得到了这个Error Image
{
"type": "about:blank",
"title": "Bad Request",
"status": 400,
"detail": "Required part 'file' is not present.",
"instance": "/user-service/recruiter/candidate/uploadDocument"
}
即使我在postman中的form data选项中传递文件。 Postman 的卷发
curl --location --request POST 'localhost:8080/user-service/recruiter/candidate/uploadDocument' \
--form 'file=@"/C:/Users/user/Downloads/Doc - Wireframe.docx"'
任何和所有的帮助是感激的
1条答案
按热度按时间nnsrf1az1#
自定义请求过滤器,使用导致错误的curl响应进行调节。