Thursday, September 20, 2018

Web Service > Upload files in the File Cabinet using Java


Customer would like to use Java and Suitetalk to upload a local file into the File Cabinet of his Netsuite account. 

Below is a sample code that demonstrates how to do this using Java and Suitetalk. The code below is for sampling purposes only.
 

Sample code snippet:

public void uploadFile() {
  try {
   String filePath = "C:\\Documents and Settings\\bsicam\\My Documents\\My Pictures\\testPDF.pdf";
   java.io.File localFile = new java.io.File(filePath);
   
         byte []buffer = new byte[4096];
         ByteArrayOutputStream ous = new ByteArrayOutputStream();
         InputStream ios = new FileInputStream(localFile);
         int read = 0;
         while ( (read = ios.read(buffer)) != -1 ) {
             ous.write(buffer, 0, read);
         }
         if(ous != null) {
          ous.close();
         }
         ios.close();
        
         byte[] byteArray = ous.toByteArray();
        
         File newFile = new File();
         newFile.setContent(byteArray);
         newFile.setName("testPDF.pdf");
         newFile.setEncoding(FileEncoding._utf8);
         newFile.setAttachFrom(FileAttachFrom._computer);
         newFile.setFileType(MediaType._PDF);
         newFile.setFolder(new RecordRef("", "-4", "", RecordType.folder));
        
         WriteResponse uploadResponse = port.add(newFile);
        
         if(uploadResponse.getStatus().isIsSuccess()) {
          console.info("Upload Successful");
         }
         else {
          console.info("Upload Failed");
         }
  } catch (Exception e) {
   console.error("Unexpected Error has occurred");
   e.printStackTrace();
  }
 }  


 

No comments:

Post a Comment