How to upload an image and save it in database? [duplicate]
Asked Answered
H

2

10

I have to create a form using JavaScript and an user will upload a JPG file and submit along with other info such as name, email, etc. When the user clicks submit all the information from the form will be loaded to a value object. For the image file I've set it to be byte[].

So assuming:

public String name;
public String email;
public byte[] logo;

I've set up a servlet as well to handle the submission but I'm not sure how to get started. How does the upload work? When user submits, how do I get the information for the image? Here's a screenshot: http://imageshack.us/f/32/77675354.png/ I need to convert that image and save it to a byte[] then convert to blob so I can insert it to a table.

Heimdall answered 11/8, 2011 at 22:25 Comment(3)
java2s.com/Code/Java/Database-SQL-JDBC/Stream-Data.htmFiske
I think it would be better to save the file to an upload directory and save the file path in the db insteadOctane
thats what i was thinking as well, but the table i use will be deleted periodically, possibly once every week also there would not be a HUGE amount of entries because i only have to make the new ads maybe 3-5 times a week so thered be at most 10 entries per week..Heimdall
S
11

For the file upload part, you need to set enctype="multipart/form-data" on the HTML form so that the webbrowser will send the file content and you'd like to use request.getPart() in servlet's doPost() method to get the file as an InputStream. For a concrete code example, see also How to upload files to server using JSP/Servlet?

Then, to save this InputStream in the DB, just use PreparedStatement#setBinaryStream() on a BLOB/varbinary/bytea column or whatever column represents "binary data" in your favorite DB engine.

preparedStatement = connection.prepareStatement("INSERT INTO user (name, email, logo) VALUES (?, ?, ?)");
preparedStatement.setString(1, name);
preparedStatement.setString(2, email);
preparedStatement.setBinaryStream(3, logo);
// ...

You don't necessarily need to convert this InputStream to byte[], it would not have been memory efficient either. Imagine that 100 user simultaneously upload images of 10MB, then 1GB of server memory would have been allocated at that point.

Sempiternal answered 11/8, 2011 at 23:0 Comment(7)
can you explain this to me: in the form i created, user choose a picture with a dir("c:\pic.png") then submits WHAT HOLDS THE DATA FOR THE PIC? i think thats the main thing imi confused about, i pass that to a servlet then its java stuff which i can handle.Heimdall
As said, the browser includes the file content in the multipart/form-data request body. Run a HTTP traffic debugger or print HttpServletRequest#getInputStream() to see it with your own eyes. On a related note, this answer is probably enlightening: stackoverflow.com/questions/81180/…Sempiternal
so i dont need to set that information to any variable? do i just do request.getAttribute(name)? sorry for such silly questions but ive never worked with uploading and downloading files etc..Heimdall
Uh, I said nowhere to use request.getAttribute(). Did you read the link to the concrete code example at end of my 1st paragraph?Sempiternal
sry.. i dont see any sort of prepared statement or connection in any of my mentor's codes i think all that is set up already.Heimdall
Ah, so it's homework and you must set it as a byte[] and then pass the value object through to something which you have no control over? Then read the InputStream into a ByteArrayOutputStream the usual Java IO way and then call toByteArray() to get the byte[].Sempiternal
I did the same thing but getting an SQLexception, java.sql.SQLException: Unknown system variable 'lower_case_table_names' and not able to resolve it.Allusive
J
2

You probably should not be storing an image in a database. The database is literally the most expensive place where you can store binary data. DB size will grow fast and querying cost is high. You might end up with non-scalable and barely efficient solution of image hosting.

Store it in separate resource server, like Amazon S3 or anywhere else (local Nginx, Tomcat etc).

Instead you can store unique file names and/or full path to the file. In such a way you'll facilitate DB's workload and columns data will be readable, so you can quickly find desired picture. I'm not even talking about performance in general, simple benchmark will easily prove it.

Jermaine answered 5/1, 2018 at 18:15 Comment(2)
yep, this is when i was coding like a scrub. now i save metadata to db, image to s3.Heimdall
I'm glad you've progressed :) I kinda missed question's date =\Jermaine

© 2022 - 2024 — McMap. All rights reserved.