Java對各種文件的操作詳解java中提供了io類庫,可以輕松的用java實現(xiàn)對文件的各種操作。下面就來說一下如何用java來實現(xiàn)這些操作。
JAVA天堂
文件下載 %> 數(shù)據(jù)庫字段中的文件下載 <%@ age contentType="text/html;charset=gb2312"%> <%@ age import="java.util.*,java.sql.*,java.io.*"%> <% tring id = request.getParameter("id"); if(id==null) { throw ew Exceptio ("沒有找到圖片"); } else { try { com.gzrealmap.lib.jdbc.JDBCUtil qlBean= com.gzrealmap.lib.jdbc.JDBCUtil.getI tance(); qlBean.co ect(); tring ql = "select * from i ernew where id = ""+79+"""; ResultSet r = qlBean.queryforUpdate(sql); rs.next(); //String fileNamed = rs.getString("imageName"); tring file= rs.getString("acc"); //String fileName = ew tring(fileNamedb.getBytes(),"iso-8859-1"); tring fileName = "a.jpg"; re o e.setHeader("Content-Di osition", "inline filename="" + fileName + """) JAVA天堂 tring filter = fileName.su tring(fileName.lastIndexOf(".")); if(filter.equals(".txt")) { re o e.setContentType("text/plain"); } else if(filter.equals(".doc")||filter.equals(".dot")) { re o e.setContentType("a lication/msword"); JAVA天堂 } else { re o e.setContentType("image/jpeg;charset=GB2312"); } ervletOutputStream o = re o e.getOutputStream(); //o.write(file); out.println(file); //o.flush(); //o.close(); qlBean.disco ect(); } catch(Exceptio ex) { out.println(ex.getMe age()); } } %> 把網(wǎng)頁保存成文件 <%@ age contentType="text/html;charset=gb2312"%> <%@ age import="java.text.*,java.util.*,java.net.*,java.io.*"%> <% URL tdURL = ull; ufferedReader tdI = ull; rintWriter tdOut = ull; try { tdURL = ew URL("http://www.163.com"); } catch (MalformedURLExceptio e) { throw e; } try { //將字節(jié)流轉(zhuǎn)變成為字符流 tdI = ew ufferedReader(new I utStreamReader(stdURL.ope tream())); tring theFileName = "c://測試//163.html"; tdOut = ew rintWriter(new ufferedWriter(new FileWriter(theFileName.toString()))); } catch (IOExceptio e) { } /***把URL指定的頁面以流的形式讀出,寫成指定的文件***/ try { tring trHtml = ""; while((strHtml = tdIn.readLine())!=null) { tdOut.println(strHtml); } } catch (IOExceptio e) { throw e; } finally { try { if(stdI != ull) tdIn.close(); if(stdOut != ull) tdOut.close(); } catch (Exceptio e) { ystem.out.println(e); } } %>
直接下載網(wǎng)上的文件 %> //然后輸出動態(tài)內(nèi)容就可以得到一個word文檔了 1,打開: 1)文件頭上加:<%@ age contentType="a lication/msword"%> xml文件里: <mime-ma ing> lt;exte io gt;doc</exte io gt; lt;mime-type>a lication/msword</mime-type> </mime-ma ing> 2)可以用js,以下代碼來自引用: <%@ age contentType="text/html;charset=gb2312" import= "java.io.*"%> <HTML> < cript> var wrd=new ActiveXObject("Word.A lication") wrd.visible=true alert ("您的"+wrd.A lication.Caption+"安裝路徑為: "+wrd.A lication.Path+" 版本號是:"+ wrd.A lication.version+" 注冊使用者是:"+wrd.A lication.UserName) wrd.Documents.Add() //wrd.Documents.Open("c:\exam.doc") wrd.Selection.TypeText("Thi i ome text.") wrd.A lication.Activate() wrd.ActiveDocument.SaveAs("c:\exam111.doc") wrd=null </script> </HTML> 2,下載: <%@ age contentType="text/html;charset=gb2312" import= "java.io.*"%> <%// 得到文件名字和路徑 tring filename = "j .doc"; tring filepath = "C:\"; // 設置響應頭和下載保存的文件名 re o e.setContentType("A LICATION/OCTET-STREAM"); re o e.setHeader("Content-Di osition","attachment filename="" + filename + """); // 打開指定文件的流信息 java.io.FileI utStream fileI utStream = ew java.io.FileI utStream(filepath + filename); //FileOutputStream out = ew FileOutputStream(filepath+"測試\" + filename); // 寫出流信息 int i; while ((i=fileI utStream.read()) != -1) { JAVA天堂 out.write(i); } fileI utStream.close(); out.close(); %> JAVA天堂 |
|