如何将文件字节数组从Java传递到.Net API并将字节数组还原到文件

时间:2019-06-26 05:28:24

标签: java c# .net rest

我需要将文件从Java程序作为字节数组传递到.NET api,然后通过该.Net API还原该文件。如果已经遇到这种情况,请有人帮忙。

我试图转换为字符串并通过,并且如下所述,但不起作用。

Java客户端代码:

private String getBytesFromFiles(String path){

    try{
      logger.info("Accessing the document====> "+path);


    File file = new File(path);
      InputStream inputStream = new FileInputStream(file);
      byte[] bytes = new byte[(int) file.length()];
      inputStream.read(bytes);
      logger.info("Byte array converted to String");
      String res = new String(bytes);
    return res;
    }catch(Exception e){
        logger.info("Error at reading the document====> "+e);
        //throw new RuntimeException(e);
    }
    return null;
}

.Net api代码:

public string  UploadToDBMultiClaims(string strDept, string strPolicyNo1, string strPolicyNo2, List<string> lstClaimNo, string strDocumentName,
        string strFileDate, string strRemark, string strCurrentUser, String btBinaryBuffer){

    byte[] bytes = new byte[btBinaryBuffer.Length * sizeof(char)];
    System.Buffer.BlockCopy(btBinaryBuffer.ToCharArray(), 0, bytes, 0, bytes.Length);
    string strDocID = "xyz"
    string path = ConfigurationManager.AppSettings["FilePath"].ToString() + DateTime.Today.ToString("yyyyMMdd") + @"\";
    string fileName = path + strDocID + ".pdf";
    System.IO.File.WriteAllBytes(fileName, bytes);

    return "1";
}

0 个答案:

没有答案