Operation of documents in Java (2)
I used to write a file in Java (http://dev.9cbs.net/develop/Article/19/19064.shtm), but not all, add some of the rest to the comment, but this time After the revision, I couldn't see it in the comment, I will post it.
10. Press the file <% @ page contentty = "text / html; charset = GB2312"%> <% @ page import = "java.io. *"%> <% fileReader myfilereader = new fileReader ("C: / Haha). txt "); BufferedReader myBufferedReader = new BufferedReader (myFileReader); String myString = null; String resultString = new String (); while ((myString = myBufferedReader.readLine ())! = null) {resultString = resultString myString "
";} Out.println (resultString); MyFileReader.close ();%>
11. Database field files download directly to the client <% @ page import = "java.sql. *"%> <% @ Page import = "java.lang. *"%> <% @ Page import = "java.io. * "%> <% @ page import. *"%> <% @ page import = "dbstep.idbManager2000. *"%> <% string filename = "bb.doc" .tostring () ; // open the database ResultSet result = null; String Sql = null; PreparedStatement prestmt = null; DBstep.iDBManager2000 DbaObj = new DBstep.iDBManager2000 (); DbaObj.OpenConnection (); // get the data in the database Sql = "select * From marklist order by markdate design; result = dbaobj.executeQuery (SQL); Result.next (); // Read data in the database InputStream in = Result.getBinaryStream ("markbody"); // Set the output Format response.reset (); resplication.setContentType ("Application / Msword"); Response.addheader ("Content-Disposition", "Attachment; FileName = /" " FileName " / "); // Removal Data in the stream BYTE [] B = New byte [1024]; int Len; while ((len = in.read (b))> 0) Response.getOutputStream (). Write (B, 0, LEN); in. CLOSE ();%> 12. Folder traversal
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "java.io. *"%>
<%
String URL1 = "C: / AAA";
File f = (New file (url1));
IF (f.IsDirectory ()) {
File [] fe = f.listfiles ();
Go_on:
For (INT i = 0; i IF (Fe [i] .Indirectory ()) { File [] fe1 = fe [i] .listfiles (); For (int J = 0; j IF (Fe1 [J] .Indirectory ()) Continue go_on; Out.println (Fe1 [J] .tostring ()); } } Else Out.println (Fe [i] .tostring ()); } } %> 13. Mobile files encoded by characters <% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ Page Import = "Java.io. *"%> <% String Ret = new string (); Try { Byte [] bytes = new byte [102400]; InputStream IN = New FileInputStream ("C: /AAA.DOC"); In.read (bytes); Ret = new sun.misc.base64encoder (). eNCode (bytes); // specific coding method In.Close (); } Catch (FilenotFoundException E) { E.PrintStackTrace (); } Catch (java.io ioexception ex) { EX.PrintStackTrace (); } Out.println (RET); Byte [] bytes = new sun.misc.base64decoder (). DecodeBuffer (RET); Java.io.byteaRrayinputStream Instream = New java.io.byteArrayInputStream (Bytes); Byte [] buffer = new byte [1444]; FileOutputStream Fs = New FileOutputStream ("D: /AAA.DOC"); INT BYTESUM = 0; INT BYTEREAD = 0; While ((Byteread = Instream.read (buffer)! = - 1) { Bytesum = byteread; fs.write (buffer, 0, byteread); } %> 14. Code files into base64 strings <% string ret = new string (); byte [] bytes = new byte [1024]; string aa = "aaaa"; BYTES = aa.getbytes (); Ret = new sun.misc. Base64encoder (). Encode (bytes); // Specific encoding method Bytes = new sun.misc.base64decoder (). DecodeBuffer (RET); AA = new string (bytes); out.println (aa);%>