Send multiple image files from Spring boot to Android

Asked 2 years ago, Updated 2 years ago, 80 views

You have already saved the file from Android to Server as MultipartHttpServletRequest. I would like to send the image and string from Server to Android again.

"response": [
        {
            "boardIdx": 29,
            "title": "Title asdsad",
            "contents": "Contents asdsad",
            "fileList": [
                {
                    "idx": 25,
                    "boardIdx": 29,
                    "originalFileName": "IMG_20200211_144336.jpg",
                    "storedFilePath": "test/20200212/52368144018200.jpg",
                    "fileSize": 147780
                },
                {
                    "idx": 26,
                    "boardIdx": 29,
                    "originalFileName": "IMG_20200211_144340.jpg",
                    "storedFilePath": "test/20200212/52368144997800.jpg",
                    "fileSize": 150788
                },
                {
                    "idx": 27,
                    "boardIdx": 29,
                    "originalFileName": "IMG_20200210_061004.jpg",
                    "storedFilePath": "test/20200212/52368145901500.jpg",
                    "fileSize": 203023
                },
                {
                    "idx": 28,
                    "boardIdx": 29,
                    "originalFileName": "IMG_20200211_144318.jpg",
                    "storedFilePath": "test/20200212/52368146848600.jpg",
                    "fileSize": 133689
                }
            ],
    }

In JSON, there are several files for each post in this way. I'd like to send you a real file in this file list.

So in the service part,

public List<TestDto> testSelectUpload(HttpServletResponse response) throws Exception{ 

List<TestFileDto> testFileDtoList;  
List<TestDto> testDtoList=mapper.testSelectUpload();

for(int i=0;i<testDtoList.size();i++){
  //This part is set according to the boardIdx
    testDtoList.get(i).setFileList(mapper.testSelectUploadFile(testDtoList.get(i).getBoardIdx()));
    testFileDtoList=mapper.testSelectUploadFile(testDtoList.get(i).getBoardIdx());

  //and return as many files as the corresponding
    for(int j=0lj<testFileDtoList.size();j++){
      String fileName=testFileDtoList.get(j).getOriginalFileName();
                byte[] files=FileUtils.readFileToByteArray(new File(testFileDtoList.get(j).getStoredFilePath()));
                response.setContentType("application/octet-stream");
                response.setContentLength(files.length);
                response.setHeader("Content-Disposition", "attachment; fileName=\"" + URLEncoder.encode(fileName,"UTF-8")+"\";");
                response.setHeader("Content_Transfer-Encoding", "binary");

                response.getOutputStream().write(files);
                response.getOutputStream().flush();
                response.getOutputStream().close();
    }
    return testDtoList;

}

I want to do it like this, but there's an error Is there a way to put everything in the response?

[Note]

Database structure.

test_board table

file the table _ test < img alt = "image" = src cloudinary. // res. : " https com image upload to / / eightcruz v 1581583611 warxl vlxlnvqjwouhzo 5.png"/>

TestDto.Java

@Data
public class TestDto{
    private int boardIdx;

...

    private List<TestFileDto> fileList;


}

TestFileDto.Java

@Data
public class TestFileDto{
    private int idx;

...

    private long fileSize


}

spring-boot file android

2022-09-21 12:42

1 Answers

HTTP does not maintain a connection, so once a response is exported, it cannot respond again. That's why there's an error (because it's disconnected).

You should either export the files one by one, or compress the target files and JSON text from the server and export them as one file.


2022-09-21 12:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.