Ntko office controls, the SSM framework for uploading files to database and page echoing

Source: Internet
Author: User

Here to use the operation of the file stream, not much to say, first put the code out

The first is the file upload to the database, where the SQL Server database is used, the database corresponds to the data type is image, and the Java entity corresponds to byte[] (bit array).

First, the same as the normal upload of files, the page to write a form forms

<form method= "POST" enctype= "Multipart/form-data" name= "Frmworddoc" >

<input type= "File" name= "UploadFile" >

</form>

Here is one of the simplest form forms, remember to mark the red place as a must.

If you use the Ntko plugin submission method is: Awordeditor (here for your own definition of the Ntko control object, adjust according to your own). Savetourl ("${pagecontext.request.contextpath}/lcgl/openws?wjid=1", "UploadFile", "" "," ", 0);

Adding the code as above in JavaScript can also be submitted. (Remember how the Ntko plugin is submitted)

@RequestMapping ("/OPENWS")
public void Openws (Integer wjid,httpservletrequest request, @RequestParam ("UploadFile") Multipartfile UploadFile) Throws unsupportedencodingexception{
PT_WSJD ws = new PT_WSJD ();

Here is the control of the file stream
InputStream InputStream;
try {
InputStream = Uploadfile.getinputstream ();
byte[] data = new byte[] {};
data = Inputstreamtobyte (InputStream);//Save the file in a byte array, the method below

The following is the same as the normal increment method, put the value into the entity
WS.SETF_WSMC (BT);
Ws.setf_wsnr (data);

//Add method
                lcglservice.insws (WS);
        } catch (IOException e) {
           & nbsp;//TODO auto-generated Catch block
            e.printstacktrace ();
        }
    }


Private byte[] Inputstreamtobyte (InputStream is) throws IOException {
Bytearrayoutputstream Baoutputstream = new Bytearrayoutputstream ();
int ch;
while ((ch = is.read ())! =-1) {
Baoutputstream.write (CH);
}
byte data [] =baoutputstream.tobytearray ();
Baoutputstream.close ();
return data; Returns byte[] Array
}

This is done by converting the file into a binary and putting it into the database.

Next we are doing the echo of the page.

@RequestMapping ("/getwsbyid")
public void Getwsbyid (Integer f_id,httpservletresponse response,httpsession session) throws ioexception{
PT_WSJD ws= Lcglservice.getwsbyid (f_id); Query the value of the database according to the ID, very common query method
Response.reset ();
Response.setcontenttype ("Applcation/octet-stream"); To determine the file format, I have a Word document stored here that can be changed on demand.
Byte[] Byt =ws.getf_wsnr (); is a field that accepts the image type of the database as a bit array, which is the data we just added to the database
Java.io.InputStream Filedatastream = new Bytearrayinputstream (byt); Data into a file stream
Javax.servlet.ServletOutputStream Myoutputstream = Response.getoutputstream (); Output stream
byte[] FileData = new byte[1024];
int readcount = 0;
while ((Readcount=filedatastream.read (filedata,0,1024))!=-1) {
Myoutputstream.write (Filedata,0,readcount);
}
Myoutputstream.flush (); Refresh Buffer Stream
Response.flushbuffer ();
}

Here I still use the Ntko plug-in to Echo

The echo in Ntko adds settimeout in JavaScript ("Awordeditor.openfromurl (' ${pagecontext.request.contextpath}/lcgl/getwsbyid '); ", 200); URL parentheses correspond to the Echo method we just wrote to echo the success.

If there are errors, but also look at the way the great God directly pointed out, accept all the criticisms and comments, if there is infringement, please contact small, small series will be in the first time to delete the infringing content, thank you.

qq:11032609

Response.reset ();
Response.setcontenttype ("Applcation/octet-stream");
Byte[] Byt =ws.getf_wsnr ();
Java.io.InputStream Filedatastream = new Bytearrayinputstream (byt);
Javax.servlet.ServletOutputStream Myoutputstream = Response.getoutputstream ();
byte[] FileData = new byte[1024];
int readcount = 0;
while ((Readcount=filedatastream.read (filedata,0,1024))!=-1) {
Myoutputstream.write (Filedata,0,readcount);
}
Myoutputstream.flush ();
Response.flushbuffer ();

Ntko office controls, the SSM framework for uploading files to database and page echoing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.