How to upload an image and saved into the database and that image should be shown on the user profile page? The image can be of any type jpg, jpeg and png. I am using JSP, jQuery and Spring MVC framework and Java and Spring data jpa.
I am not using servlet in my application. I am new to this field and not able to complete it.
You need a VARBINARY column to contain the image. Open the file using an InputStream, load byte[]s from it, and write that into the column.
You need another column to save the mime file type. You can obtain the file type with java.nio.file.Files.probeContentType( Path path )
In your response headers, you need to:
Use setContentLength() to set the length of the file.
Use setContentType() to set the mime image type.
If your database offers a means to create an InputStream on a varbinary column, use it. Otherwise, you need to read the contents of your varbinary column into a byte[], and then create a ByteArrayInputStream on the byte[].
Finally, you need to construct a response entity using the constructor that accepts an input stream: return new ResponseEntity(inputStream, httpHeaders, HttpStatus.OK);
Related
I am trying to upload metadata to SharePoint server using ValidateUpdateListItem method. In this method, value of bNewDocumentUpdate is true to avoid creating a new version for the uploaded file. The name of metadata which has to be updated, is 'cc1'. When i fetch metadata information, the name of metadata is converted from 'cc1' to 'OData__x0063_c1'. Now, i have used both the names (encoded and decoded) to upload the metadata, but below error is generated -
{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"Column 'cc1' does not exist. It may have been deleted by another user. /Shared Documents"}}}
Earlier, i was not using ValidateUpdateListItem method, and was able to upload metadata using encoded name.
Is there any way to upload the encoded metadata while using ValidateUpdateListItem method?
Update -
This problem has been resolved by using 'InternalName' of metadata. Earlier i was using 'EntityPropertyName' of metadata to set metadata value.
Until now I passed unmodified blob data as an InputStream:
public InputStream getData(Blob blob) {
return blob.getData().getBinaryStream();
}
Now I want to pass a modified stream with following characteristics:
Add additional content from iterators before and after the blob data
The blob data contains line breaks (CRLF). I want to add content before and after each line.
We are forced to use Java 7 in our project. Can anyone tell me what is the easiest and most performant way to do this?
I want to write an excel and send it to the user as a response in an application using Play framework 1.x. But I am not sure how to set the response content-type/MIME-type for returning doc or excel file.
Let me know the steps for this.
From the documentation:
To serve binary data, such as a file stored on the server, use the renderBinary method. For example, if you have a User model with a play.db.jpa.Blob photo property, add a controller method to load the model object and render the image with the stored MIME type:
public static void userPhoto(long id) {
final User user = User.findById(id);
response.setContentTypeIfNotSet(user.photo.type());
java.io.InputStream binaryData = user.photo.get();
renderBinary(binaryData);
}
I have never saved and retrieved an image to and from the database before. I wrote down what I guessed would be the process. I would just like to know if this is correct though:
Save image:
Select & Upload image file from jsp (Struts 2) which will save it as a .tmp file.
Convert the .tmp file to a byte[] array (Java Server-Side)
Store the byte[] array as a blob in the database (Java Server-Side)
Get image:
Get the byte[] array from the database (Java Server-Side)
Convert the byte[] array to an image file (Java Server-Side)
Create the file in a location (Java Server-Side)
Use an img tag to display the file (JSP Client-Side)
Delete the file after it's finished being used? (Java Server-Side)
I'm aware of the fact that it is highly recommended to not save & retrieve images to and from the database. I would like to know how to do it anyway.
Thanks
Almost correct.
It's expensive and not so great to create the file on the fly and then delete it.
Yes, you store it as the raw bytes in the database, but the way to retrieve it and display it to a client machine is to implement a web handler that sets the content-type of the response to the appropriate MIME type and then dumps the bytes out to the response stream.
Yes, You get it right.
Save Image :
The decision to save image is very much dependent on further usage. You have one option to save the file on the file system. The location for saved file should be saved into the metadata in the database table.
Get Image:
You do not have to right file data on any temp location. It can be easily rendered from the database only. Just send a request from client and intercept that request in a spacial designed Servlet. This Servlet will read the file metadata and corresponding file, if successful, write the file back on the response stream.
I have a Struts2 jsp page their i am sending one image, Temporary file path is comming to my java class after form submission but i do not know how can to save that path in db by changing it to Blob type.. Please consider this image columns is of blob type in my database table..
Here is the output what am getting in my Javaclass after the form submission:
My image path:
F:\Documents and Settings\software.netbeans\7.0\apache-tomcat-7.0.11_base\work\Catalina\localhost\AIGSA\upload__214d4f3e_136e8b74d9c__7fff_00000021.tmp 105542
filenames:
* Winter.jpg
Code:
for (File u: repImage)
{
System.out.println("*** "+u+"\t"+u.length());
}
int saveToDb= mo.addMembers(memberName, repImage);
How can I send my form Image to this {repImage Name, so that it will be easy to save it so my db
am not sure, but try:
int saveToDb= mo.addMembers( memberName, repImage.getBytes() );
If I understand your question properly, you want to store the binary data in your database. I'd say this is a bad idea in the first place for multiple reasons. A better method would be to reference a relative path that you can then use in your web application or the file system directly. Better yet, just store the reference to the location in Amazon/S3 where you want to save it/use it.
Regardless, to answer your question you would want to use ByteArrayOutputStream.