Base91 を使用して、画像を文字列として mysql データベースに保存しています。文字列を取得して画像に戻すと、文字列がデータベースによって破損したようです。
写真を撮り、それを Base91byte[]に変換し、byte[]を文字列に変換し (Latin1 を使用)、文字列を に戻しbyte[]、次に画像、そしてそれはうまくいきました。真ん中のサーバーで試してみると、画像が壊れてしまいます。
データベースの特定の列の照合をutf8からascii、latin1に変更しようとしましたが、うまくいきません。
コード:
エンコーディング画像:
File file1 = new File(path); FileInputStream in = new FileInputStream(file1); byte[] imagesBytes = new byte[(int) file1.length()]; in.read(imagesBytes, 0, (int) file1.length()); _picture = new String(Base91.encode(imagesBytes), Base91.CHARSET); //appointment.addPicture(new String(Base91.encode(imagesBytes), Base91.CHARSET));エンコードされた画像の保存:
JsonObject jsonx = new JsonObject(); jsonx.addProperty("picture", request.getParameter("picture")); db.insert("AppointmentPicture", "(AppointmentID, picture)values(?,?)", new Object[]{appointment.getDbID(), jsonx.toString()}, false);画像の取得:
ResultSet rSet = db.query("AppointmentPicture", new String[]{"picture"}, "AppointmentID = ?", new Object[]{appointmentID}); System.out.println("pictures grabbed"); JsonObject json = new JsonObject(); int counter = 0; while (rSet.next()) { counter++; json.addProperty(String.valueOf(counter), rSet.getString("picture")); } json.addProperty("count", String.valueOf(counter)); response.getWriter().write(json.toString()); System.out.println("pictures returned");デコード画像:
String serverResponse = in.readLine(); JSONObject json = new JSONObject(serverResponse); int count = Integer.parseInt(json.getString("count")); for(int i = 0; i < count; i++){ JSONObject j = new JSONObject(json.getString(String.valueOf(i + 1))); byte[] picBytes = j.getString("picture").getBytes(Base91.CHARSET); File file = new File(imageDirectory, String.valueOf(System.currentTimeMillis()) + ".jpeg"); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); out.write(picBytes, 0, picBytes.length); }