可以通过以下步骤实现:
@PostMapping("/upload")
public String uploadImage(@RequestParam("image") MultipartFile image) {
try {
byte[] imageData = image.getBytes();
// 将字节数组发送到Python服务
// ...
return "Image uploaded successfully.";
} catch (IOException e) {
return "Failed to upload image.";
}
}
from flask import Flask, request
app = Flask(__name__)
@app.route('/upload', methods=['POST'])
def upload_image():
image = request.files['image']
image.save('path/to/save/image.jpg')
return 'Image uploaded successfully.'
if __name__ == '__main__':
app.run()
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class ImageUploader {
public static void main(String[] args) {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("http://python-service/upload");
try {
byte[] imageData = // 从Spring Boot应用获取字节数组
HttpEntity entity = new ByteArrayEntity(imageData);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
这样,通过Flask将图像文件从Spring Boot Java发送到Python的过程就完成了。在实际应用中,可以根据需求对图像进行处理、存储或其他操作。
领取专属 10元无门槛券
手把手带您无忧上云