java中提供了io類庫
新建目錄
<%@ page contentType=
<%
//String URL = request
String filePath=
filePath=filePath
java
if(!myFilePath
myFilePath
%>
新建文件
<%@ page contentType=
<%@ page import=
<%
String filePath=
filePath=filePath
File myFilePath=new File(filePath);
if(!myFilePath
myFilePath
FileWriter resultFile=new FileWriter(myFilePath);
PrintWriter myFile=new PrintWriter(resultFile);
String content =
String strContent = content
myFile
resultFile
%>
刪除文件
<%@ page contentType=
<%
String filePath=
filePath=filePath
java
if(myDelFile
{
myDelFile
out
}
else
{
out
}
%>
文件拷貝<%@ page contentType=
<%@ page import=
<%
int bytesum=
int byteread=
//file:讀到流中
InputStream inStream=new FileInputStream(
FileOutputStream fs=new FileOutputStream(
byte[] buffer =new byte[
int length;
while ((byteread=inStream
{
out
bytesum+=byteread;
out
fs
}
inStream
%>
整個文件夾拷貝
<%@ page contentType=
<%@ page import=
<%String url
String url
(new File(url
File[] file=(new File(url
for(int i=
if(file[i]
file[i]
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(url
byte[] b=new byte[
int len;
while((len=input
output
}
output
output
input
}
}
%>
文件下載
<%@ page contentType=
<%@ page import=
<%
String fileName =
//讀到流中
InputStream inStream=new FileInputStream(
//設置輸出的格式
response
response
response
//循環取出流中的數據
byte[] b = new byte[
int len;
ServletOutputStream outStream = response
while((len=inStream
outStream
outStream
outStream
inStream
%>
數據庫字段中的文件下載
<%@ page contentType=
<%@ page import=
<%
String id = request
if(id==null)
{ throw new Exception (
}
else
{
try
{
com
nnect();
String sql =
ResultSet rs = SqlBean
rs
//String fileNamedb = rs
String file= rs
//String fileName = new String(fileNamedb
String fileName =
response
String filter = fileName
if(filter
{
response
}
else if(filter
{
response
}
else
{
response
}
ServletOutputStream o = response
//o
out
//o
//o
SqlBean
}
catch(Exception ex)
{
out
}
}
%>
把網頁保存成文件
<%@ page contentType=
<%@ page import=
<%
URL stdURL = null;
BufferedReader stdIn = null;
PrintWriter stdOut = null;
try {
stdURL = new URL(
}
catch (MalformedURLException e) {
throw e;
}
try {
//將字節流轉變成為字符流
stdIn = new BufferedReader(new InputStreamReader(stdURL
String theFileName =
stdOut = new PrintWriter(new BufferedWriter(new FileWriter(theFileName
}
catch (IOException e) {
}
/***把URL指定的頁面以流的形式讀出
try {
String strHtml =
while((strHtml = stdIn
stdOut
}
}
catch (IOException e) {
throw e;
}
finally {
try {
if(stdIn != null)
stdIn
if(stdOut != null)
stdOut
}
catch (Exception e) {
System
}
}
%>
直接下載網上的文件
<%@ page contentType=
<%@ page import=
<%@ page import=
<%
int bytesum=
int byteread=
URL url = new URL(
byte[] buffer =new byte[
while ((byteread=inStream
{
out
bytesum+=byteread;
//System
fs
}
%>
按行讀文件
<%@ page contentType=
<%@ page import=
<%
FileReader myFileReader=new FileReader(
BufferedReader myBufferedReader=new BufferedReader(myFileReader);
String myString=null;
String resultString=new String();
while((myString=myBufferedReader
resultString=resultString+myString+
}
out
myFileReader
%>
對word文檔的處理(上傳與下載)
<%@ page contentType=
<!
<%
response
// response
//以上這行設定傳送到前端浏覽器時的檔名為test
//就是靠這一行
%>
//然後輸出動態內容就可以得到一個word文檔了
xml文件裡
<mime
<extension>doc</extension>
<mime
</mime
<%@ page contentType=
<HTML>
<script>
var wrd=new ActiveXObject(
wrd
alert (
wrd
//wrd
wrd
wrd
wrd
wrd=null
</script>
</HTML>
<%@ page contentType=
<%// 得到文件名字和路徑
String filename =
String filepath =
// 設置響應頭和下載保存的文件名
response
response
// 打開指定文件的流信息
java
//FileOutputStream out = new FileOutputStream(filepath+
// 寫出流信息
int i;
while ((i=fileInputStream
out
}
fileInputStream
out
%>
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27570.html