Cannot get inputStream from web url. Why?

I'm trying to create a book with an image in JUnit test using MockMultipartFile. But for unknown reasons the stream cannot be taken. Why?
@Test
@Order(2)
void testCreateBookWithAnImage() {
BookRequestDTO bookDto = new BookRequestDTO();
bookDto.setTitle("New Book With Image");
bookDto.setAuthor("Name Surname");
bookDto.setSummary("Instructions how to add an image and asociate it with the Book object");
bookDto.setIsbn("555556666");
bookDto.setYear(2023);
bookDto.setPages(20);
bookDto.setCirculation(3);
bookDto.setCategory("Novel");

MultipartFile file = null;

try {
file = createMultipartFileFromURL("https://cdn.pixabay.com/photo/2014/09/05/18/32/old-books-436498_1280.jpg");
// file = createMultipartFileFromURL("https://pngimg.com/uploads/pineapple/pineapple_PNG2755.png");
} catch (IOException e) {
e.printStackTrace();
}

try {
Book createdBook = bookService.addNewBookWithImage(bookDto, file);
assertEquals("New Book With Image", createdBook.getTitle());
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
@Order(2)
void testCreateBookWithAnImage() {
BookRequestDTO bookDto = new BookRequestDTO();
bookDto.setTitle("New Book With Image");
bookDto.setAuthor("Name Surname");
bookDto.setSummary("Instructions how to add an image and asociate it with the Book object");
bookDto.setIsbn("555556666");
bookDto.setYear(2023);
bookDto.setPages(20);
bookDto.setCirculation(3);
bookDto.setCategory("Novel");

MultipartFile file = null;

try {
file = createMultipartFileFromURL("https://cdn.pixabay.com/photo/2014/09/05/18/32/old-books-436498_1280.jpg");
// file = createMultipartFileFromURL("https://pngimg.com/uploads/pineapple/pineapple_PNG2755.png");
} catch (IOException e) {
e.printStackTrace();
}

try {
Book createdBook = bookService.addNewBookWithImage(bookDto, file);
assertEquals("New Book With Image", createdBook.getTitle());
} catch (IOException e) {
e.printStackTrace();
}
}
8 Replies
JavaBot
JavaBot2y ago
This post has been reserved for your question.
Hey @Tomasm21! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Tomasm21
Tomasm21OP2y ago
Lets continue code: createMultipartFileFromURL :
public static MockMultipartFile createMultipartFileFromURL(String imageUrl) throws IOException {
URL url = new URL(imageUrl);
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);

try (InputStream inputStream = connection.getInputStream()) {
BufferedImage image = ImageIO.read(inputStream);

// Extract image properties
String format = getImageFormat(image);
String contentType = getContentType(format);
String fileExtension = getFileExtension(format);
System.out.println("\nBookServiceTest: File size: "+inputStream.readAllBytes().length);
System.out.println("BookServiceTest: Format: "+format);
System.out.println("BookServiceTest: Content type: "+contentType);
System.out.println("BookServiceTest: File extension: "+fileExtension+"\n");

// Convert image to MockMultipartFile
return new MockMultipartFile("The book", "The book." + fileExtension, contentType, inputStream);
} catch (IOException e) {
// Handle the exception when there is no internet connection or openStream fails
e.printStackTrace();
throw new IOException("Failed to retrieve the image from the URL: " + imageUrl, e);
}
}
public static MockMultipartFile createMultipartFileFromURL(String imageUrl) throws IOException {
URL url = new URL(imageUrl);
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);

try (InputStream inputStream = connection.getInputStream()) {
BufferedImage image = ImageIO.read(inputStream);

// Extract image properties
String format = getImageFormat(image);
String contentType = getContentType(format);
String fileExtension = getFileExtension(format);
System.out.println("\nBookServiceTest: File size: "+inputStream.readAllBytes().length);
System.out.println("BookServiceTest: Format: "+format);
System.out.println("BookServiceTest: Content type: "+contentType);
System.out.println("BookServiceTest: File extension: "+fileExtension+"\n");

// Convert image to MockMultipartFile
return new MockMultipartFile("The book", "The book." + fileExtension, contentType, inputStream);
} catch (IOException e) {
// Handle the exception when there is no internet connection or openStream fails
e.printStackTrace();
throw new IOException("Failed to retrieve the image from the URL: " + imageUrl, e);
}
}
Tomasm21
Tomasm21OP2y ago
Then this part:
System.out.println("\nBookServiceTest: File size: "+inputStream.readAllBytes().length);
System.out.println("BookServiceTest: Format: "+format);
System.out.println("BookServiceTest: Content type: "+contentType);
System.out.println("BookServiceTest: File extension: "+fileExtension+"\n");
System.out.println("\nBookServiceTest: File size: "+inputStream.readAllBytes().length);
System.out.println("BookServiceTest: Format: "+format);
System.out.println("BookServiceTest: Content type: "+contentType);
System.out.println("BookServiceTest: File extension: "+fileExtension+"\n");
results in:
where
c1_0.id=?

BookServiceTest: File size: 0
BookServiceTest: Format: jpg
BookServiceTest: Content type: image/jpeg
BookServiceTest: File extension: jpg

Hibernate:
select
where
c1_0.id=?

BookServiceTest: File size: 0
BookServiceTest: Format: jpg
BookServiceTest: Content type: image/jpeg
BookServiceTest: File extension: jpg

Hibernate:
select
Why is that?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Tomasm21
Tomasm21OP2y ago
ohh.. Its temporary. Ok I will read all bytes to byte array
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot2y ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one.
Want results from more Discord servers?
Add your server