Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: image 버그 해결중 #58

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,21 @@ public String getFilename() {
}
}

public void sendImageRequest(UploadImageRequestDto requestDto, boolean isProduct)
throws IOException {
public void sendImageRequest(UploadImageRequestDto requestDto, boolean isProduct) throws IOException {
// URL 선택: 제품인지 농장인지에 따라 다름
String url = isProduct ? PRODUCT_URL : FARM_URL;

// HTTP 헤더 설정: 멀티파트 폼 데이터로 설정
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);

// URI 빌드
URI uri = UriComponentsBuilder.fromUriString(backAdaptorProperties.getAddress())
.path(url)
.build()
.toUri();

// 요청 본문 생성
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("id", requestDto.getId());

Expand All @@ -107,8 +110,10 @@ public String getFilename() {
});
}

// HttpEntity 생성
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(body, headers);

// 서버로 요청 보내기
try {
ResponseEntity<Void> response = restTemplate.exchange(
uri,
Expand All @@ -117,11 +122,13 @@ public String getFilename() {
Void.class
);

// 응답 상태 확인
if (response.getStatusCode() != HttpStatus.CREATED) {
throw new IOException("Failed to upload thumbnail. Status code: " + response.getStatusCode());
}

} catch (Exception e) {
// 예외 처리
throw new IOException("Error writing request body to server", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import com.wefood.front.chat_bot.adaptor.ChatBotAdaptor;
import com.wefood.front.product.dto.UploadImageRequestDto;
import com.wefood.front.product.dto.UploadThumbnailRequestDto;
import java.awt.Image;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/wefood/front/user/adaptor/UserAdaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ public void farmCreate(FarmRequest farmRequest, Long id) {
}

public void farmImageCreate(MultipartFile[] multipartFiles, Long id) throws IOException {

System.out.println(multipartFiles[1].getName());
String url = backAdaptorProperties.getAddress() + "/api/farm/upload-part";

System.out.println(url);
// URL에 id를 쿼리 파라미터로 추가
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
.queryParam("id", id);

// MultiValueMap을 사용하여 멀티파트 요청 본문 구성
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();

// 파일들을 본문에 추가
for (MultipartFile file : multipartFiles) {
body.add("files", new FileSystemResource(convert(file)));
}
System.out.println();

// HTTP 헤더 설정
HttpHeaders headers = new HttpHeaders();
Expand Down
Empty file added uploads/test
Empty file.
Loading