Java HTTP Post Applet 服务器 - 内部生成图像

发布于 2024-10-06 02:46:40 字数 306 浏览 6 评论 0原文

我在小程序中使用 J2D 创建了 BufferedImage。 我想使用 HTTP Post @ http://localhost:3001/upload/file 上传此 BufferedImage。

编辑:我有一个 ROR 服务器处理服务器端的事情,我正在寻找客户端的 Java 代码。

我能找到的所有示例都涉及上传文件。

有人知道如何上传 BufferedImage 吗?

干杯,

槽型

I have a BufferedImage created using J2D in an applet.
I want to upload this BufferedImage using HTTP Post @ http://localhost:3001/upload/file.

EDIT: I have a ROR server handling the serverside of things, I am looking for the Java code for the client.

All the examples I can find involve uploading Files.

Anybody know how to upload a BufferedImage?

Cheers,

slotishtype

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我喜欢麦丽素 2024-10-13 02:46:40

好的,下面是创建 bufferedimage 的代码,将其编码为 Base64 字符串,然后使用 apache commons 库通过 http 将字符串发布到 ROR 服务器。

        BufferedImage bi = new BufferedImage(110, 110, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bi.createGraphics();
        AffineTransform saveTX = new AffineTransform();
        saveTX.translate(translateX, translateY);
        saveTX.scale(0.2, 0.2);
        g2.setTransform(saveTX);
        this.paint(g2);

        ImageInputStream bigInputStream = ImageIO.createImageInputStream(bi);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bi, "BMP", baos);
        byte[] bytes = baos.toByteArray();
        String dataImg = new Base64().encodeBase64String(bytes);

        PostMethod post = new PostMethod("http://localhost:3001/upload/file");

        post.addParameter("upload[test]", dataImg);

        HttpClient client = new HttpClient();
        int status = client.executeMethod(post);

        g2.dispose();

ROR 服务器只是获取字符串,对其进行解码并将其保存到硬盘......

require "base64"
 class UploadController < ApplicationController

 #Token = nil
 skip_before_filter :verify_authenticity_token 

 def index
    render :file => 'app\views\upload\uploadfile.html.erb'
 end
 def file

File.open('test.gif', 'wb') do|f|
  f.write(Base64.decode64(params[:upload][:test]))
end

    render :text => "File has been uploaded successfully"
 end

结束

感谢大家的帮助,

slotishtype

OK, So here is the code that creates the bufferedimage, encodes it as a Base64 string and then using the apache commons library posts the string over http to a ROR Server.

        BufferedImage bi = new BufferedImage(110, 110, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bi.createGraphics();
        AffineTransform saveTX = new AffineTransform();
        saveTX.translate(translateX, translateY);
        saveTX.scale(0.2, 0.2);
        g2.setTransform(saveTX);
        this.paint(g2);

        ImageInputStream bigInputStream = ImageIO.createImageInputStream(bi);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bi, "BMP", baos);
        byte[] bytes = baos.toByteArray();
        String dataImg = new Base64().encodeBase64String(bytes);

        PostMethod post = new PostMethod("http://localhost:3001/upload/file");

        post.addParameter("upload[test]", dataImg);

        HttpClient client = new HttpClient();
        int status = client.executeMethod(post);

        g2.dispose();

The ROR server simply takes the string, decodes it and saves it to the harddrive....

require "base64"
 class UploadController < ApplicationController

 #Token = nil
 skip_before_filter :verify_authenticity_token 

 def index
    render :file => 'app\views\upload\uploadfile.html.erb'
 end
 def file

File.open('test.gif', 'wb') do|f|
  f.write(Base64.decode64(params[:upload][:test]))
end

    render :text => "File has been uploaded successfully"
 end

end

Thanks for the help guys,

slotishtype

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文