Hi,
I am new to java web development. I am working on a application which has an requirement like uploading a word document in the database. I got some solutions how to read a document and write the contents in database but, requirement is like uploading a document in database. Can anyone help me with an idea how and where can i start with.
Thanks in advance.
Thanks,
Hari
Are you asking how to read the uploaded file?
Hi
First you will need to add to the upload page an input file tag in the form like this
<inpt type="file" name="myfile" />then you will change the tag of the form and add the following
enctype="multipart/form-data" method="post"in the action attribute of the form put a servlet
in the doPost of the servlet you will need a library such as apache file upload
http://commons.apache.org/fileupload/using.html
you will call this library and give it the request and it will give u an input stream for the file
read the file into an object
convert the object into byte[]
create a CLOB field in the table in oracle
use the something like the following code to insert into the CLOB field
// Inserting CLOB value with PreparedStatement.setBytes()PreparedStatement ps = con.prepareStatement(
"INSERT INTO FileTable(ID, Subject, Body) VALUES (2,?,?)");
ps.setString(1, subject);
byte[] bodyIn = yourObject.toByteArray;
ps.setBytes(2, bodyIn);
int count = ps.executeUpdate();
ps.close();
Hope I could Help
The Amazing Java
Uploading a Word document in Sql or Oracle database - Old proved
Hi!
this is a scriptlet of upload/download of BLOBs on Oracle DB. This solution have some years (was original developed for a Ora8i and tested later on 9 and 10), and was tunned for Very Large Blobs (>500Mb) using jdbc only. You see in this solution that Blob content never is stored on memory, which gives a very good scalability.
I erase all error management on the examples, please you MUST have a LOT of error management!!
UPLOAD
public int write(String id, InputStream is) {
Connection con = getConnection();
//
// create a new record with empty blob
String sql = "INSERT INTO "+getTableName()+" (ID,"+getBlobFieldName()+") VALUES (?,empty_blob())";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
int rowsAffected = pstmt.executeUpdate();
pstmt.close();
//
// Get empty blob for update
sql = "SELECT "+getFieldName()+" FROM "+getTableName()+" WHERE ID="+id+" FOR UPDATE";
pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
rs.next();
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob(1);
OutputStream os = blob.getBinaryOutputStream();
//
// fill blob
byte[] buf = new byte[1024];
int len = -1;
int len_total = 0;
while ( (len=is.read(buf,0,1024)) != -1) {
os.write(buf,0,len);
len_total += len;
}
//
// close resources
os.close();
rs.close();
pstmt.close()
con.close()
//
// return writed bytes
return len_total;
}
- The InputStream of Document can be get from HTTPRequest.
- Be carefull with cast to oracle.sql.BLOB on Application Servers JEE 1.4 o later, this use was because Oracle have a Bug with the homonym function on JDBC Blob class.
DOWNLOAD
public int read(OutputStream os) {
String sql = "SELECT "+getBlobFieldName()+" FROM "+getTableName()+" WHERE "+getConstraint();
Connection con = getConnection();
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
rs.next();
java.sql.Blob blob = rs.getBlob(1);
InputStream is = blob.getBinaryStream();
byte[] buf = new byte[1024];
int len = -1;
int len_total = 0;
while ( (len=is.read(buf,0,1024)) != -1) {
os.write(buf,0,len);
len_total += len;
}
is.close();
rs.close();
pstmt.close()
con.close()
return len_total;
}
//
// This is a Servlet doGet method
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(getMimeType());
response.setHeader("Content-Disposition","in-line;filename=\""+getFilename()+"\"");
java.io.OutputStream out = response.getOutputStream();
read(out);
out.flush();
out.close();
}
enjoy,
Julio.
Very nice solution
Hi Julio,
This is a very nice solution to insert an empty record in the file table and then get an output stream to it and write your data
but I think you agree with me that this suits more the large files than small ones
I mean 100K doc files are better saved directly in the insert statement than the input stream
Thanks for your contribution and sharing your knowledge with us
Muhammad Safwat.
The Amazing JAVA
RE: Very nice solution
Hi Muhammad,
this is a streaming solution, designed with two scenarios in mind.
1) Very Large Blobs: Media files, Satellite media and somethings like that
2) High Load / High Concurrency: This solution applies in cases with small files but high load (for example, in your case if you have a concurrency of 10 users, then you must have a 1000Kb of buffer!!)
Keep in mind than sometimes users edit small Word documents and when the store became really big. If you design you solution waiting small documents, then check the size in upload servlet code.
Share is Good!!!!
enjoy,
Julio
Nice post about Word
Nice post about Word document. I like your blog very much because it has very interesting articles of different topics like 642-426 questions, 642-373 questions and 642-145 questions and their tips and tricks. I am a very big googler and search on different topics. Between searching i found your nice blog. Thanks for your this great blog.
i agree with mtv134, that is
i agree with mtv134, that is the best reply possible.
industry logos | infinity logo design | Packaging Design
most of the people over here
most of the people over here are learners themselves, but still the people who are expert have helped us alot.
Annual Report Design | CD Cover Design
great document.
great document.
Thanks a lot
No better way i could have understood :)
Thanks for the document
Era
The life insurance policy
The life insurance policy with stress to the rural areas and to the socially and economically backward classes.that i like to discus about this post in my blog thanks for sharing the info.
Regards,
Lie - online health insurance quotes
Post new comment