Webservice没有收到AKKA发送的pdf文件

时间:2018-06-08 01:29:40

标签: scala pdf akka-stream akka-http


我正在尝试使用AKKA作为客户端将XML和PDF发送到Web服务(java + spring)。我收到一条关于PDF文件的消息:“PDF文件是无效文件或是空白”,但在补偿中,XML文件被读取(我可以访问日志)。 我已经和网络服务提供商讨论了这种情况,他们说我需要发送PDF文件为application/x-www-form-urlencoded,但不幸的是,我没有成功。 (我已尝试使用application/pdfmultipart/form-dataapplication/octet-stream
我使用TestMultipartFileUpload.scala为例。

我的 ent 输出为:HttpEntity.Chunked(multipart/form-data; boundary=ftl4gn7+7xJE6ToWG+3aROCk)

我的 req 输出为:HttpRequest(HttpMethod(POST),http://webservice/upload,List(date, hashAlgorithm, Authorization),HttpEntity.Chunked(multipart/form-data; boundary=ftl4gn7+7xJE6ToWG+3aROCk),HttpProtocol(HTTP/1.1))

httpEntityPDF

Future(Success(HttpEntity.Strict(application/x-www-form-urlencoded,ByteString(37, 80, 68, 70, 45, 
49, 46, 52, 10, 37, -30, -29, -49, -45, 10, 49, 32, 48, 32, 111, 98, 106, 10, 60, 60, 10, 47, 84, 
105, 116, 108, 101, 32, 40, -2, -1, 0, 80, 0, 101, 0, 116, 0, 105, 0, -25, 0, -29, 0, 111, 0, 32, 
0, 84, 0, 101, 0, 115, 0, 116, 0, 101, 41, 10, 47, 65, 117, 116, 104, 111, 114, 32, 40, -2, -1, 
0, 114, 0, 111, 0, 98, 0, 115, 0, 111, 0, 110, 41, 10, 47, 67, 114, 101, 97, 116, 111, 
114, 32, 40, -2)... and [51276] more)))

我的 pdfFile

FormData.BodyPart.Strict(xyz.pdf,HttpEntity.Strict(application/x-www-form-urlencoded,ByteString(37, 
80, 68, 70, 45, 49, 46, 52, 10, 37, -30, -29, -49, -45, 10, 49, 32, 48, 32, 111, 98, 106, 10, 60, 
60, 10, 47, 84, 105, 116, 108, 101, 32, 40, -2, -1, 0, 80, 0, 101, 0, 116, 0, 105, 0, -25, 0, -29, 
0, 111, 0, 32, 0, 84, 0, 101, 0, 115, 0, 116, 0, 101, 41, 10, 47, 65, 117, 116, 104, 111, 114, 32, 
40, -2, -1, 0, 114, 0, 111, 0, 98, 0, 115, 0, 111, 0, 110, 41, 10, 47, 67, 114, 101, 97, 116, 111, 
114, 32, 40, -2)... and [51276] more),Map(xyz.pdf -> xyz.pdf),List())

我无法想象在Web服务端发生的事情是忽略PDF文件,或者是否可能,事实上,PDF文件不会发生。 如果有人有建议,我会很感激。


def createEntity(file: Array[File]): Future[RequestEntity] = {
  val contentTypePDF = MediaTypes.`application/x-www-form-urlencoded`.toContentTypeWithMissingCharset
  val contentTypeXML = MediaTypes.`text/xml`.withCharset(HttpCharsets.`UTF-8`)
  val httpEntityPDF = HttpEntity(contentTypePDF, file(1).length(), FileIO.fromPath(file(1).toPath)).toStrict(10 seconds)
  val httpEntityXML = HttpEntity(contentTypeXML, file(0).length(), FileIO.fromPath(file(0).toPath))
  val xmlFile = Multipart.FormData.BodyPart("document", httpEntityXML, Map.empty)
  val pdfFile = Multipart.FormData.BodyPart.Strict(file(1).getName, Await.result(httpEntityPDF, 10 seconds), Map(file(1).getName -> file(1).getName))
  val formData = Multipart.FormData(xmlFile, pdfFile)
  Marshal(formData).to[RequestEntity]
}

val datePrefix = headers.RawHeader(HEADER_DATE_PREFIX, auth.headerTimeStamp)
val algothPrefix = headers.RawHeader(HEADER_ALGOTH_PREFIX, ALGORITHM)
val spHeader = headers.RawHeader(AUTHORIZATION, getSpHeader)
val listHeader = List(datePrefix, algothPrefix, spHeader)
val target = Uri(scheme = "http", authority = Uri.Authority(Uri.Host("???")), path = Uri.Path("/upload"))

def createRequest(target: Uri): Future[HttpRequest] = {
  for {
    ent <- createEntity(files)
    _ = println(ent.toString())
  } yield HttpRequest(
    HttpMethods.POST,
    uri = target,
    headers = listHeader,
    entity = ent
  )
}

val result = for {
  req <- createRequest(target)
  resp <- Http().singleRequest(req)
  responseBodyAsString <- Unmarshal(resp).to[String]
} yield responseBodyAsString

val r = Await.result(result, 100 seconds)

0 个答案:

没有答案