Stack Exchange
Sending request from Java to Box for uploading files
I want to upload a file to Box using Java HTTP RESTFUL requests.
My code:
File file = new File("C:/Users/Developer/Desktop/cloud.ppt");           
Base64.Encoder encoder = Base64.getEncoder();
Base64.Decoder decoder = Base64.getDecoder();                   
String boundary1 = "----------------------------741e90d31eff";
String header1 = "--"+boundary1+"\nContent-Disposition: form-data; name=\"file\"; filename="+file.getName()+";\nContent-Type: application/octet-stream";
System.out.println("=====header1======="+header1);
String footer1 = "--"+boundary1+"--"; 
String header2= header1;
header1 = header1+"\r\n\r\n";
byte[] byteConvert = header1.getBytes("UTF-8");
String headerEncoded1 = encoder.encodeToString(byteConvert);
HttpResponse response;          
System.out.println("******headerEncoded1*****"+headerEncoded1);                     
byte[] byteConvert2 = header2.getBytes("UTF-8");            
while(headerEncoded1.endsWith("="))
{
    header2+=' ';
    header2 = header2+"\r\n\r\n";
    byteConvert2 = header2.getBytes("UTF-8");
    headerEncoded1 = encoder.encodeToString(byteConvert2);
}   

byte fileContent[] = new byte[(int)file.length()];
System.out.println("fileContent[]: " +fileContent);
FileInputStream fin = new FileInputStream(file);
fin.read(fileContent);
fin.close();
String fileContentString = new String(fileContent);                     
String fileBody = fileContentString;
byteConvert2 = fileBody.getBytes("UTF-8");
System.out.println("byteConvert2: " +byteConvert2);
String bodyEncoded1 = encoder.encodeToString(byteConvert2);
System.out.println("***bodyEncoded1***"+bodyEncoded1);          
String bodyBlob = null;         
String last4Bytes = bodyEncoded1.substring(bodyEncoded1.length()-4,bodyEncoded1.length());
System.out.println("****last4Bytes****"+last4Bytes);

if(last4Bytes.endsWith("==")) 
{
    System.out.println("=== if ===");
    last4Bytes = last4Bytes.substring(0,2) + "0K";
    bodyEncoded1 = bodyEncoded1.substring(0,bodyEncoded1.length()-4) + last4Bytes;
    byteConvert2 = footer1.getBytes("UTF-8");;             
    String footerEncoded = encoder.encodeToString(byteConvert2);
    String Combine = headerEncoded1+bodyEncoded1+footerEncoded;
    byteConvert2 = Combine.getBytes("UTF-8");
    byte[] byteConvert3 = decoder.decode(byteConvert2);             
    bodyBlob = byteConvert3.toString();
    System.out.println("\n***if bodyBlob***"+bodyBlob);
}

else if(last4Bytes.endsWith("=")) 
{
    System.out.println("===else if bodyBlob===");
    last4Bytes = last4Bytes.substring(0,3) + "N";
    bodyEncoded1 = bodyEncoded1.substring(0,bodyEncoded1.length()-4) + last4Bytes;
    footer1 = "\n" + footer1;
    byteConvert2 = footer1.getBytes("UTF-8");
    String footerEncoded = encoder.encodeToString(byteConvert2);                
    String strCombineNew= headerEncoded1+bodyEncoded1+footerEncoded;
    byteConvert2 = strCombineNew.getBytes("UTF-8");             
    byte[] byteConvert3 = decoder.decode(byteConvert2);         
    bodyBlob = byteConvert3.toString();
    System.out.println("\n****else if bodyBlob***"+bodyBlob);
} 
else 
{
    System.out.println("===else===");
    footer1 = "\r\n" + footer1;
    byteConvert2 = footer1.getBytes("UTF-8");
    String footerEncoded = encoder.encodeToString(byteConvert2);
    String strCombine = headerEncoded1+bodyEncoded1+footerEncoded;
    byteConvert2 = strCombine.getBytes("UTF-8");               
    byte[] byteConvert3 = decoder.decode(byteConvert2);             
    bodyBlob = byteConvert3.toString();
    System.out.println("\n***else***"+bodyBlob);
}
    
//System.out.println("****bodyBlob***"+bodyBlob);
//java.sql.Blob blob = org.hibernate.Hibernate.createBlob(bodyBlob.getBytes());
//System.out.println("=====blob======"+blob);

String strFolderId = "XXXXXXX";         
HttpClient httpclient = HttpClientBuilder.create().build();
String sUrl = "https://upload.box.com/api/2.0/files/content?parent_id="+strFolderId;
HttpPost request = new HttpPost(sUrl);

request.setEntity(new StringEntity(bodyBlob));
request.setHeader("Content-Type","multipart/form-data; boundary="+boundary1);
request.setHeader("Authorization", "Bearer" +strAccessTokenOnly);
            
System.out.println("\n=== Content-Length ===" +String.valueOf(fileBody.length()));

request.setHeader("Content-Length",String.valueOf(fileBody.length()));
response = httpclient.execute(request);         
System.out.println("\n=== response ==="+response.getStatusLine());

Here, if I set the header with Content-Length, the execution will stop midway.
And if I do not add the setHeader, I get a Bad Request, Status 400.
If anyone has worked on something related to this scenario, please help me.
Thanks in advance.

If you use Java 7+, and if you are supposedly talking about box.com, have a look here

Thanks for quick response. But I have seen this type of example before. I don't want to use SDK libraries. Do you have any code snippet without using SDK libraries.

No, not really, sorry... Except "have a look at how I do it and reproduce" :(
