Как сохранить изображение в виде строки byte [] в базе данных без использования multipartfile? - PullRequest
0 голосов
/ 28 февраля 2020

Когда я отправляю изображение в виде строки base64 в формате json с помощью почтальона, я получаю сообщение об ошибке «Текущий запрос не является составным запросом». Я сохраняю изображение в виде типа BLOB-объекта в базе данных. Мы можем отправить изображение в виде строки base64, не используя составной запрос. Пожалуйста, помогите мне решить эту ошибку.

Это мой класс обслуживания

@Component
public class StudentProfileService {
@Autowired
StudentProfileRepository profileRepo;

private AmazonS3 amazonS3;

@Value("${aws.access.key.id}")
private String accessKey;

@Value("${aws.access.key.secret}")
private String secretKey;

@Value("${aws.region}")
private String awsRegion;

@Value("${aws.s3.audio.bucket}")
private String s3Bucket;

@Value("${aws.endpointUrl}")
private String endpointUrl;

@SuppressWarnings("deprecation")
@PostConstruct
private void initializeAmazon() {

    AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);


    this.amazonS3 = new AmazonS3Client(credentials);
}

public String uploadFile(MultipartFile multipartFile) {
    String fileUrl = "";
    try {
        File file = convertMultiPartToFile(multipartFile);
        String fileName = generateFileName(multipartFile);
        fileUrl = endpointUrl + "/" + s3Bucket + "/" + fileName;
        uploadFileTos3bucket(fileName, file);
        file.delete();
    } catch (Exception e) {
       e.printStackTrace();
    }
    return fileUrl;
}

private File convertMultiPartToFile(MultipartFile file) throws IOException {
    File convFile = new File(file.getOriginalFilename());
    FileOutputStream fos = new FileOutputStream(convFile);
    fos.write(file.getBytes());
    fos.close();
    return convFile;
}

private String generateFileName(MultipartFile multiPart) {
    return  multiPart.getOriginalFilename().replace(" ", "_");
}

private void uploadFileTos3bucket(String fileName, File file) {
    amazonS3.putObject(new PutObjectRequest(s3Bucket, fileName, file)
            .withCannedAcl(CannedAccessControlList.PublicRead));
}

public StudentProfile storeFile(StudentProfile stdprofile, MultipartFile file) {
    String fileName = StringUtils.cleanPath(file.getOriginalFilename());
    String fileUrl = endpointUrl + "/" + s3Bucket + "/" + fileName;;
    byte[] image = null;
    try {
        image = Base64.encodeBase64(file.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
    stdprofile = new StudentProfile(stdprofile.getFullName(), stdprofile.getRollNo(), stdprofile.getGender(), stdprofile.getClasslist(), stdprofile.getClassTeacher(), stdprofile.getBirthday(), stdprofile.getMobile(), stdprofile.getEmail(), stdprofile.getFatherName(), stdprofile.getMotherName(), stdprofile.getBusNo(), stdprofile.getStatus(), image, fileName, fileUrl);

    return profileRepo.save(stdprofile);
}
}

Это мой контроллер Класс

@Controller
public class StudentProfileController {
@Autowired
StudentProfileService profileService;

@PostMapping("/stdprofile")
@ResponseBody
public String uploadFile(StudentProfile stdprofile, @RequestPart(value = "file") MultipartFile file) {
    stdprofile = profileService.storeFile(stdprofile, file);
    return this.profileService.uploadFile(file);
}
}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...