0

I am developing an application in spring-boot which uploads the file in a searched folder. Even though I have followed all instructions given the program is not working. It is not identifying the folder even though the folder is present.

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.box.sdk.BoxAPIConnection;
import com.box.sdk.BoxAPIException;
import com.box.sdk.BoxFile;
import com.box.sdk.BoxFolder;
import com.box.sdk.BoxItem;
import com.box.sdk.BoxSearch;
import com.box.sdk.BoxSearchParameters;
import com.box.sdk.PartialCollection;


@RestController
public class Upload_file {
    @Autowired //using @Autowired we can automatically inherit the properties of componnent class,i.e we can automatically wire/inject dependencies into the class without explicit instantion or congiguration
    Config_file cf;
    
    @PostMapping("/upLoad")//we put the path for this conntroller
    public String upload(@RequestParam("file") MultipartFile file){
        
        BoxAPIConnection api = cf.create();//we create api using create method mentioned in cONFIG_FILE COMPONENT CLASS
        System.out.println(api.getAccessToken());
        System.out.println("Connection established");
        
        
        try {
            System.out.println("Entering try block");
            
            long offsetValue = 0;
            long limitValue = 100;
            BoxSearch boxSearch = new BoxSearch(api);//api.getAccessToken()
            System.out.println("Box Search API  "+boxSearch.getAPI());
            BoxSearchParameters searchParams = new BoxSearchParameters();
            searchParams.setQuery("Rishil_testing");
            searchParams.setType("folder");
            System.out.println("Folder set");
            System.out.println("Set query type and type: "+searchParams.getType());
            
            PartialCollection<BoxItem.Info> searchResults = boxSearch.searchRange(offsetValue, limitValue, searchParams);
            System.out.println("Defining Partila Collection");
            BoxFolder rootFolder=  BoxFolder.getRootFolder(api);
            System.out.println(" root folder Defined");
            
            System.out.println("entering for loop "+searchResults.size());
            
            for ( BoxItem.Info  iteminfo : searchResults) {
                System.out.println("inside for loop");
                if(iteminfo instanceof BoxFolder.Info) {
                    System.out.println("hiiiiiii222222222");

                    BoxFolder.Info folderinfo=(BoxFolder.Info) iteminfo;
                    System.out.println("hiiiiiii111111111 folderinfo.getID(): "+folderinfo.getID());
                    BoxFolder folder=new BoxFolder(api,folderinfo.getID());
                    System.out.println("hiiiiiii333333333");
                    BoxFile.Info fileInfo = folder.uploadFile(file.getInputStream(), file.getName());
                    System.out.println("hiiiiiii444444444");
                }
            }
        }
        catch (BoxAPIException e) {
            System.out.println("Box API Exception: " + e.getMessage());
            e.printStackTrace();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return "File uploaded successfully tis time";
        
    }

}

I tried all the following methods-

  1. Verify the search query and folder type: Double-check the values you set for the search query and folder type ("Rishil_testing" and "folder" respectively). Ensure that the folder you are searching for matches the query and has the correct type.

  2. Check the authentication and permissions: Make sure that the API connection (BoxAPIConnection) used for searching and accessing the folder has the necessary authentication credentials and sufficient permissions to perform the operations. Ensure that you have proper authentication and access token handling in place.

  3. Confirm the folder ID: Check if the folder ID you are using to create the BoxFolder object is correct. Verify that the folder ID corresponds to the desired folder you want to upload the file to.

  4. Handle errors and exceptions: Surround the code block within the for loop with try-catch blocks to handle any exceptions that may occur during the folder identification or file upload process. Log any exceptions or error messages to understand the cause of the issue.

  5. Debug and log messages: Use logging statements or debugging tools to trace the execution flow and print relevant information. This will help you identify any intermediate states or variables that might be causing the problem.

  6. Test with a different folder: Try searching and uploading the file to a different folder to see if the issue persists. This can help determine if the problem is specific to the folder you are currently targeting.

  7. Validate API responses: Print or log the API responses or information retrieved during the search process. This will provide insights into the data received from the API and help identify any inconsistencies or unexpected results.

By following these suggestions, you should be able to pinpoint the issue and resolve it. If the problem persists, consider sharing more details about your code, any error messages or exceptions you encounter, and the specific API or SDK you are using. This additional information can help in providing more targeted assistance.

Greg
  • 16,359
  • 2
  • 34
  • 44

0 Answers0