java - Store and retrieve word documents with MySQL -
i need store , retrieve ms word documents mysql 5.1 servlets. i've code upload file, don't know can feed table. i've used blob field i've insert .doc files.
here's code snippet upload files:
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); try { // access file uploaded client part p1 = request.getpart("file"); string type=p1.getcontenttype(); string name=p1.getname(); long size = p1.getsize(); inputstream = p1.getinputstream(); //fileinputstream fis = is. // read filename sent part part p2 = request.getpart("name"); scanner s = new scanner(p2.getinputstream()); string filename = s.nextline(); // read filename stream // filename use on server string outputfile = this.getservletcontext().getrealpath(filename); // path on server fileoutputstream os = new fileoutputstream (outputfile); // write bytes taken uploaded file target file int ch = is.read(); while (ch != -1) { os.write(ch); ch = is.read(); } os.close(); out.println("<h3>file : '" + name + "' type : '" + type + "' " + "of size : " + ((double) size/1024) + "kb uploaded successfully!</h3>"); } catch(exception ex) { out.println("exception -->" + ex.getmessage()); } { out.close(); } }
here, i've used servlets 3.0 feature uploading file... table schema :
resources - userid [varchar(15)] - document [mediumblob]
could me how can store document table , though blob type representing binary data, how can retrieve word document (*.doc)?
a partial answer on storing word documents in files:
don't need additional column save file name document's record id can serve file name.
when saving new document, in database transaction can undo process when goes wrong.
in pseudo code, this:
begin transaction; try { save new record document; save word document in predefined directory, using record's id filename; } catch (exception e) { rollback transaction; throw e; // rethrow exception } commit transaction;
the code above assumes exception thrown when error occurs.
Comments
Post a Comment