问题
Here is my code. I get a blob from my database. Which is returned to me like this: java.io.BufferedInputStream@16e31e37. Now im trying to display the image from my servlet in the browser however my BufferedImage image is always null. I dubugged it and notice my blob length is always 34.. Not matter the image.
@Path("/photo" )
public class DisplayPhoto {
@GET
@Path("{id}")
@Produces("image/*")
public Response post(@PathParam("id") String id) throws IOException {
Connection con = connection();
Blob blob = getPhoto(con);
int blobLength = 0;
try {
blobLength = (int) blob.length();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
byte[] data = null;
try {
data = blob.getBytes(1, blobLength);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedImage image = null;
try {
image = ImageIO.read(new ByteArrayInputStream(data));
// ImageIO.write(image, "JPEG", new File("C:/Users/Nicolas/Desktop")); // writing image to some specific folder.
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return Response.ok(image).build();
}
public Connection connection(){
Connection con = null;
try {// set up driver for database
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:mysql://aa1c9da17owdhky.cotr7twg0ekb.us-west-2.rds.amazonaws.com/test","nightskycode","uocb4t111");
// boolean reachable = con.isValid(10);// check for connection to DB
} catch (SQLException e) {
e.printStackTrace();
System.out.println("No go!3");
}
return con;
}
public Blob getPhoto(Connection con){
Blob photo = null;
Statement stmt = null;
ResultSet rs = null;
try {
stmt = con.createStatement();
rs = stmt.executeQuery("Select photo from photos where photo_id = 7");
if (stmt.execute("Select photo from photos where photo_id = 7")) {
rs = stmt.getResultSet();
while (rs.next()) { // results here
photo = rs.getBlob("photo");
System.out.println(rs.getString("photo")); }
}
}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
return photo;
}
Here is how I am uploading my data to the database
@Path("/submitinfo" )
public class SubmitName {
@POST
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String post(@FormDataParam("file") InputStream uploadedInputStream, @FormParam("first") String name) {
Connection con = connection();
postName(con, name);
postPhoto(con, uploadedInputStream);
return name;
}
public void postPhoto(Connection con, InputStream uploadedInputStream){
Statement stmt = null;
String updateQuery = "INSERT INTO photos (photo) values ('" + uploadedInputStream + "')" ;
System.out.println(updateQuery);
try {
stmt = con.createStatement();
stmt.executeUpdate(updateQuery);
con.close(); // Close the connection
} catch (SQLException e) {
e.printStackTrace();
}
}
回答1:
The problem is how you're trying to store data as a blob. You need to use a PreparedStatement - specifically, with its setBlob() method:
String mySQL = "INSERT INTO photos (photo) values (?)";
PreparedStatement pStmt = con.prepareStatement(mySQL);
pStmt.setBlob(1, uploadedInputStream);
pStmt.execute();
Edit to add: The reason it isn't working now is because you're concatenating a String
to create your SQL and you're getting the result of InputStream.toString()
which is what you see being stored - it's the default Object.toString()
which is a combination of the class name and hashcode.
And really you should always use PreparedStatement
s. Not only does it make for cleaner code, it handles quoting/escaping for you which is less error prone.
来源:https://stackoverflow.com/questions/20736688/imageio-read-returns-null-on-byte-array