File upload 使用IDS V3将文件上载到Quickbooks online

File upload 使用IDS V3将文件上载到Quickbooks online,file-upload,quickbooks,intuit-partner-platform,File Upload,Quickbooks,Intuit Partner Platform,我想使用IDS V3将文件上载到QuickBooks Online。 我遵循了这里描述的步骤 . 这是我的源代码 URL url = new URL("https://quickbooks.api.intuit.com/v3/company/My_company_ID/upload"); HttpURLConnection request = (HttpURLConnection) url.openConnection(); request.setDoOutput(true);

我想使用IDS V3将文件上载到QuickBooks Online。 我遵循了这里描述的步骤 . 这是我的源代码

 URL url = new URL("https://quickbooks.api.intuit.com/v3/company/My_company_ID/upload");
    HttpURLConnection request = (HttpURLConnection) url.openConnection();
    request.setDoOutput(true);
    request.setRequestMethod("POST");
    HttpParameters para = new HttpParameters();
    //String status = URLEncoder.encode("中 文","utf-8").replaceAll("\\+", "%20");
    //para.put("status", status);
    String boundary = "---------------------------37531613912423";
    //String content = "--"+boundary+"\r\nContent-Disposition: form-data; name=\"status\"\r\n\r\n";
    String pic = "\r\n--"+boundary+"\r\nContent-Disposition: form-data; name=\"pic\"; filename=\"postpic.gif\"\r\nContent-Type: image/gif\r\n\r\n";
    byte[] end_data = ("\r\n--" + boundary + "--\r\n").getBytes();  
    File f = new File("/Users/cnanfack/Documents/oml_map1.gif");
    FileInputStream stream = new FileInputStream(f);
    byte[] file = new byte[(int)f.length()];
    stream.read(file);
    String lineEnd = "\r\n";
    String header =   "Content-Disposition: form-data; name=\"file_metadata_0\""+lineEnd
    +"Content-Type: application/xml; charset=UTF-8"+lineEnd
    +"Content-Transfer-Encoding: 8bit"+lineEnd;

   String attachable =  header+"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Attachable xmlns=\"http://schema.intuit.com/finance/v3\" domain=\"QBO\" sparse=\"false\">"
    +"<EntityRef type=\"Bill\">285</EntityRef>"
    +"<Size>4099</Size>"
    +"<ContentType>image/gif</ContentType>"
    +"<FileName>postpic.gif</FileName>"
    +"</Attachable>";

    request.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary); 
    //request.setRequestProperty("Content-Length", String.valueOf(boundary.getBytes().length+"test".getBytes().length+pic.getBytes().length+f.length()+end_data.length)); 
    //consumer.setAdditionalParameters(para);
      consumer.sign(request);
      OutputStream ot = request.getOutputStream();
      ot.write(end_data);
      ot.write(attachable.getBytes());
      ot.write(end_data);
      //ot.write(status.getBytes());
      ot.write(pic.getBytes());
      ot.write("Content-Transfer-Encoding: binary\r\n\r\n".getBytes());
      ot.write(file);
      ot.write(end_data);
      ot.flush();
      ot.close();
    System.err.println("Sending request...");
    request.connect();
    System.err.println("Response: " + request.getResponseCode() + " "
          + request.getResponseMessage());
    BufferedReader reader =new BufferedReader(new InputStreamReader(request.getInputStream()));
    String b = null;
    while((b = reader.readLine())!=null){
       System.err.println(b);
    }
URL=新URL(“https://quickbooks.api.intuit.com/v3/company/My_company_ID/upload");
HttpURLConnection请求=(HttpURLConnection)url.openConnection();
请求。设置输出(true);
请求。setRequestMethod(“POST”);
HttpParameters para=新的HttpParameters();
//字符串状态=URLEncoder.encode(“中 文","utf-8“).replaceAll(“\\+”,“%20”);
//第15段“地位”,地位;
字符串边界=“------------------------------------37531613912423”;
//String content=“-->+boundary+”\r\n内容处理:表单数据;名称=\“status\”\r\n\r\n;
String pic=“\r\n--”+boundary+“\r\n内容处理:表单数据;名称=\“pic\”文件名=\“postpic.gif\”\r\n内容类型:image/gif\r\n\r\n”;
byte[]end_data=(“\r\n--”+boundary+“--\r\n”).getBytes();
文件f=新文件(“/Users/cnanfack/Documents/oml_map1.gif”);
FileInputStream=新的FileInputStream(f);
字节[]文件=新字节[(int)f.length()];
stream.read(文件);
字符串lineEnd=“\r\n”;
String header=“内容处置:表单数据;名称=\”文件\元数据\ 0\”+lineEnd
+“内容类型:应用程序/xml;字符集=UTF-8”+lineEnd
+“内容传输编码:8位”+行结束;
字符串可附加=标题+“”
+"285"
+"4099"
+“图像/gif”
+“postpic.gif”
+"";
request.setRequestProperty(“内容类型”、“多部分/表单数据”;boundary=“+boundary”);
//request.setRequestProperty(“Content Length”,String.valueOf(boundary.getBytes().Length+“test”.getBytes().Length+pic.getBytes().Length+f.Length()+end_data.Length));
//消费者。设置附加参数(第段);
消费者签名(请求);
OutputStream ot=request.getOutputStream();
ot.写入(结束_数据);
ot.write(attachable.getBytes());
ot.写入(结束_数据);
//写入(status.getBytes());
ot.write(pic.getBytes());
写入(“内容传输编码:二进制\r\n\r\n”.getBytes());
ot.write(文件);
ot.写入(结束_数据);
ot.flush();
ot.close();
System.err.println(“发送请求…”);
request.connect();
System.err.println(“响应:+request.getResponseCode()+”)
+request.getResponseMessage());
BufferedReader=new BufferedReader(new InputStreamReader(request.getInputStream());
字符串b=null;
而((b=reader.readLine())!=null){
系统错误println(b);
}
返回的结果是:

Response: 200 OK
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2013-11-10T20:24:27.823-08:00"/>
响应:200 OK
但我在Quickbooks Online上没有看到该文件。 也许我错过了什么。有人能帮我吗?
提前感谢您

我已经使用Java Devkit尝试了这个上传端点调用。它工作得很好。PFB详细信息

 -  Request URI : https://quickbooks.api.intuit.com/v3/company/688779980/upload? Http
 -  Method : POST Content-Disposition: form-data; name="file_metadata_2bddf" 
 -  Content-Type: application/xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Attachable xmlns="http://schema.intuit.com/finance/v3">
    <FileName>images.jpg</FileName>
    <ContentType>image/jpeg</ContentType>
    <Lat>25.293112341223</Lat>
    <Long>-21.3253249834</Long>
    <PlaceName>Fake Place</PlaceName>
    <Note>Attachable note 900a49e1</Note>
    <Tag>Attachable tag 900a49e1</Tag>
</Attachable>
JAVA/.Net SDK下载链接-

希望它会有用

编辑-添加Java代码片段

public static void executeUploadForImageFilePoc(DataService service) throws ParseException, FMSException, FileNotFoundException {
    System.out.println("executeUploadForImageFile");
    String uuid = UUID.randomUUID().toString().substring(0, 8);
    Attachable attachable1 = new Attachable();
    //attachable.setSize(new Long("34234"));
    attachable1.setLat("25.293112341223");
    attachable1.setLong("-21.3253249834");
    attachable1.setPlaceName("Fake Place");
    attachable1.setNote("Attachable note " + uuid);
    attachable1.setTag("Attachable tag " + uuid);
    Attachable attachable = attachable1;
    attachable.setFileName("images.jpg");
    attachable.setContentType("image/jpeg");
    File file = new File("C:\\images_new.jpg");
    InputStream in = new FileInputStream(file);

    //Upload endpoint call
    Attachable attachableOutput = service.upload(attachable, in);

    //Check return XML
    attachable.getFileAccessUri();
    System.out.println(attachableOutput.getFileAccessUri());
    System.out.println(attachableOutput.getFileName());

    //Download endpoint call
    InputStream output = service.download(attachableOutput);
    try {
        BufferedImage bImageFromConvert = ImageIO.read(output);

        ImageIO.write(bImageFromConvert, "jpg", new File("C:\\images_new_manas.jpg"));

    } catch (IOException e) {
        throw new FMSException("Error while reading the upload file.", e);
    }
}

谢谢

我在dotnet中有一个工作代码。看看它是否适合您:

DataService.DataService commonServiceQBO= new DataService.DataService(qboContextoAuth);
      string imagePath = string.Concat(AppDomain.CurrentDomain.BaseDirectory, "\\", "Services\\Resource\\image.jpg");
                        System.IO.FileInfo file = new System.IO.FileInfo(imagePath);
                        Attachable attachable = new Attachable();
                        attachable.AttachableRef = new AttachableRef[1];
                        attachable.AttachableRef[0].EntityRef = new ReferenceType();
                        attachable.AttachableRef[0].EntityRef.type = "Bill";
                        attachable.AttachableRef[0].EntityRef.Value = "12";
                        attachable.Lat = "25.293112341223";
                        attachable.Long = "-21.3253249834";
                        attachable.PlaceName = "Fake Place";
                        attachable.Note = "Attachable note123 ";
                        attachable.Tag = "Attachable tag123 ";
                        using (System.IO.FileStream fs = file.OpenRead())
                                {
                                    attachable.ContentType = "image/jpeg";
                                    attachable.FileName = file.Name;
                                    Attachable attachableUploaded = commonServiceQBO.Upload(attachable, fs);
                                }
下载使用:
byte[]responseByte=service.Download(已附加)

谢谢你的回复。你能和我分享一下你正在使用的Intuit JAVA SDK对象吗?。我选中了Attachable object,但它似乎只用于附加便笺,我可以看到可以在哪里指定要上载的文件。谢谢,我已经更新了我的主要帖子。请看一看,让我知道它是如何运行的。很高兴知道它正在工作:)请将此帖子标记为ans。。感谢关于可附加对象的上载示例代码存在问题,
public static void executeUploadForImageFilePoc(DataService service) throws ParseException, FMSException, FileNotFoundException {
    System.out.println("executeUploadForImageFile");
    String uuid = UUID.randomUUID().toString().substring(0, 8);
    Attachable attachable1 = new Attachable();
    //attachable.setSize(new Long("34234"));
    attachable1.setLat("25.293112341223");
    attachable1.setLong("-21.3253249834");
    attachable1.setPlaceName("Fake Place");
    attachable1.setNote("Attachable note " + uuid);
    attachable1.setTag("Attachable tag " + uuid);
    Attachable attachable = attachable1;
    attachable.setFileName("images.jpg");
    attachable.setContentType("image/jpeg");
    File file = new File("C:\\images_new.jpg");
    InputStream in = new FileInputStream(file);

    //Upload endpoint call
    Attachable attachableOutput = service.upload(attachable, in);

    //Check return XML
    attachable.getFileAccessUri();
    System.out.println(attachableOutput.getFileAccessUri());
    System.out.println(attachableOutput.getFileName());

    //Download endpoint call
    InputStream output = service.download(attachableOutput);
    try {
        BufferedImage bImageFromConvert = ImageIO.read(output);

        ImageIO.write(bImageFromConvert, "jpg", new File("C:\\images_new_manas.jpg"));

    } catch (IOException e) {
        throw new FMSException("Error while reading the upload file.", e);
    }
}
DataService.DataService commonServiceQBO= new DataService.DataService(qboContextoAuth);
      string imagePath = string.Concat(AppDomain.CurrentDomain.BaseDirectory, "\\", "Services\\Resource\\image.jpg");
                        System.IO.FileInfo file = new System.IO.FileInfo(imagePath);
                        Attachable attachable = new Attachable();
                        attachable.AttachableRef = new AttachableRef[1];
                        attachable.AttachableRef[0].EntityRef = new ReferenceType();
                        attachable.AttachableRef[0].EntityRef.type = "Bill";
                        attachable.AttachableRef[0].EntityRef.Value = "12";
                        attachable.Lat = "25.293112341223";
                        attachable.Long = "-21.3253249834";
                        attachable.PlaceName = "Fake Place";
                        attachable.Note = "Attachable note123 ";
                        attachable.Tag = "Attachable tag123 ";
                        using (System.IO.FileStream fs = file.OpenRead())
                                {
                                    attachable.ContentType = "image/jpeg";
                                    attachable.FileName = file.Name;
                                    Attachable attachableUploaded = commonServiceQBO.Upload(attachable, fs);
                                }