如何在java中将Image转换为base64字符串?

时间:2016-04-08 05:19:01

标签: java image base64

这可能是重复但我在将图像转换为Base64以便将其发送到Http Post时遇到了一些问题。我试过这段代码,但它给了我错误的编码字符串。

 public static void main(String[] args) {

           File f =  new File("C:/Users/SETU BASAK/Desktop/a.jpg");
             String encodstring = encodeFileToBase64Binary(f);
             System.out.println(encodstring);
       }

       private static String encodeFileToBase64Binary(File file){
            String encodedfile = null;
            try {
                FileInputStream fileInputStreamReader = new FileInputStream(file);
                byte[] bytes = new byte[(int)file.length()];
                fileInputStreamReader.read(bytes);
                encodedfile = Base64.encodeBase64(bytes).toString();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return encodedfile;
        }

输出: [B @ 677327b6

但我在许多在线编码器中将同一图像转换为Base64,并且它们都给出了正确的大Base64字符串。

编辑:如何重复?与我的重复的链接并没有给我转换字符串我想要的解决方案。

我在这里缺少什么?

3 个答案:

答案 0 :(得分:23)

问题是您将toString()的调用返回到Base64.encodeBase64(bytes),返回一个字节数组。所以你最终得到的是字节数组的默认字符串表示,它对应于你得到的输出。

相反,你应该这样做:

encodedfile = new String(Base64.encodeBase64(bytes), "UTF-8");

答案 1 :(得分:7)

我想你可能想要:

String encodedFile = Base64.getEncoder().encodeToString(bytes);

答案 2 :(得分:4)

这对我有用。您可以将输出格式的选项更改为Base64.Default。

public class TestScreen extends ScreenAdapter {

    private final Body body;
    private int V_WIDTH = 320;
    private int V_HEIGHT = 480;
    private int PPM = 100;

    private SpriteBatch batch;
    private OrthographicCamera camera;
    private World world;
    private Sprite sprite;
    Box2DDebugRenderer box2DDebugRenderer;

    public TestScreen(){
        batch = new SpriteBatch();
        camera = new OrthographicCamera();
        camera.setToOrtho(false, V_WIDTH / PPM, V_HEIGHT / PPM);
        camera.position.set(0,0,0);
        world = new World(new Vector2(0,0) , true);

        sprite = new Sprite(new Texture("test/player.png"));
        box2DDebugRenderer = new Box2DDebugRenderer();

        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.KinematicBody;
        body = world.createBody(bodyDef);
        FixtureDef fixtureDef = new FixtureDef();

        PolygonShape shape = new PolygonShape();
        shape.setAsBox(sprite.getWidth()/2 / PPM, sprite.getHeight()/2 / PPM);
        fixtureDef.shape = shape;
        body.createFixture(fixtureDef);

        sprite.setPosition(body.getPosition().x - sprite.getWidth() /2 ,body.getPosition().y - sprite.getHeight() / 2  );
    }

    @Override
    public void render(float delta) {
        super.render(delta);
        camera.position.set( body.getPosition().x, body.getPosition().y , 0);
        camera.update();
        world.step(1/60.0f, 6, 2);
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        sprite.draw(batch);
        batch.end();
        box2DDebugRenderer.render(world, camera.combined);
    }
}