Когда я загружаю файл из почтальона, он работает.
Когда я загружаю его из внешнего интерфейса, я получаю следующую ошибку:
Код: Выделить всё
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
Вот код службы angular, откуда я загружаю файл:
Код: Выделить всё
@Injectable({
providedIn: 'root'
})
export class PostFileServiceService {
constructor(private http: HttpClient) { }
public postFile(name: string, size: any, type: any): Observable{
const body = JSON.stringify({name, size, type});
const Options = {
headers: new HttpHeaders({
'content-type':'multipart/form-data', 'boundary':'----WebKitFormBoundaryG8vpVejPYc8E16By'
})
}
const url = `http://localhost:8080/api/v1/file/upload?name=${name}&size=${size}&type=${type}`;
return this.http.post(url, body, Options);
}
}
но боюсь, что я написал неправильно.
Вот код из класса контроллера в Java, где я управляю вызовами на серверную часть:
Код: Выделить всё
@RestController
@RequestMapping(path="api/v1/file")
@CrossOrigin(origins = "http://localhost:4200")
public class filecontroller {
@Autowired
private final FileSystemStorageService filesService;
public filecontroller(FileSystemStorageService filesService) {
this.filesService = filesService;
}
@PostMapping("/upload")
public ResponseEntity uploadFiles(@RequestParam MultipartFile file){
String message = "";
try{
filesService.store(file);
message = "File Uploaded successfully: "+file.getOriginalFilename();
return ResponseEntity.status(HttpStatus.OK).body(new responseMessage(message));
}catch (Exception e){
message = "Could not upload the file: "+file.getOriginalFilename() + "!";
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new responseMessage(message));
}
}
@GetMapping("/files")
public ResponseEntity getListFiles(){
List files = filesService.getAllFiles().map(dbFile->{
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath().path("/files/").path(dbFile.getId()).toUriString();
return new fileResponse(dbFile.getFileName(), dbFile.getFileSize().length, dbFile.getType(), fileDownloadUri);
}).collect(Collectors.toList());
return ResponseEntity.status(HttpStatus.OK).body(files);
}
@GetMapping("/files/{id}")
public ResponseEntity getFile(@PathVariable String id){
fileToUpload file = filesService.getFile(id);
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""+file.getFileName()+"\"").body(file.getFileSize());
}
}
Если вам нужен другой фрагмент кода, спросите меня.
Подробнее здесь: https://stackoverflow.com/questions/790 ... va-angular