我正在尝试从我的Groovy控制器发送一个pdf文件,由我的Flask端点处理。我想将其编码为 b64 格式,并在我的 Flask 端点解码和访问该文件。pdf 文件来自我的 gsp 文件。关于我如何做同样的事情的任何建议?
时髦控制器:
def extractDocument(){
def file = params.filename
if (file instanceof MultipartFile && file != null){
byte[] encoded = Base64.getEncoder().encodeToString(file.getBytes());
println(encoded)
try{
//request_generator(encoded)
HttpURLConnection post = (HttpURLConnection) new URL("http://127.0.0.1:5000/uploadDoc").openConnection();
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Authorisation", "basic")
//OutputStream
post.getOutputStream().write(encoded)
def postRC = post.getResponseCode()
println(postRC)
if(postRC == 200) {
println(post.getInputStream().getText());
}
else{
println("Not connect")
}
普惠制:
<g:uploadForm url= "[controller:'Document', action:'extractDocument']" method="post">
<input type="file" id="myFile" name="filename">
<input type="submit" value="Submit">
</g:uploadForm>
烧瓶应用:
@app.route('/uploadDoc', methods = ['POST', 'GET'])
def upload_file():
print('Hello')
file1 = request.data
file2 = base64.b64decode(file1)
print(file2)
print(file1)
print(request.method)
尝试发送文件时,我在 Flask 应用程序上得到以下结果: 你好 b'' b'' 发布
这将是一个很难回答的问题,因为我不确定有多少开发人员会说Python和Groovy,并且可能同时使用这两种使用的框架。
因此,我提供了一些通用的调试技术,而不是代码中的解决方案,当我遇到此类问题时,我会使用这些技术。
限制问题范围
基本上,问题可能至少在三个方面。
- 时髦代码
- 蟒蛇代码
- 网络运输
我会尝试缩小问题范围。
根据您的知识和偏好...
时髦代码
不幸的是,我不是Groovy开发人员。
您可以尝试为您的代码编写测试,这样您就可以确保它正在执行您期望的操作。
另外,我可能会重构代码。您的extractDocument
不仅是提取,还要做几件事,包括转换和发送。这使得测试变得困难。
你可以继续阅读Single Responsibility Principle
和Dependency Injection
.
蟒蛇代码
如上所述,尝试编写测试。
或者启动调试器。这可以通过在行上插入import pdb;pdb.set_trace()
来轻松完成,然后再变得有趣。例如,在您的print(hello)
之后。
网络
还可以通过curl
将一些输入直接发送到终结点。
这可能不是您期望的答案,但是在尝试这样做时,您可以自己找出问题,并在将来帮助自己。