Java HTTP Post Applet server - Internally generated Image

夙愿已清 提交于 2019-12-22 01:31:19

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/4349335/java-http-post-applet-server-internally-generated-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!