<%-- 功能:JSP例程 - 在JSP中创建目录 调用方式: path --> 文件路径 Mkdir(String path) 返回信息: 操作成功返回为空 操作失败返回失败原因。 --%>

<%@ page contentType="text/html; charset=gb2312" %>

<%@ page import="java.io.*" %> <%! String Mkdir(String path) {

String msg=null; java.io.File dir;

// 新建文件对象 dir =new java.io.File(path); if (dir == null) { msg = "错误原因:
对不起,不能创建空目录!"; return msg; } if (dir.isFile()) { msg = "错误原因:
已有同名文件" + dir.getAbsolutePath() + "存在。"; return msg; }

if (!dir.exists()) { boolean result = dir.mkdirs(); if (result == false) { msg = "错误原因:
目录" + dir.getAbsolutePath() + "创建失败,原因不明!"; return msg; } // 如果成功创建目录,则无输出。 msg ="成功创建目录: " + dir.getAbsolutePath() + ""; return msg; }else { msg = "错误原因:
目录" + dir.getAbsolutePath() + "已存在。"; } return msg; } %> <% String filepath = "usr/home/hoyi/html/dir"; String opmsg = Mkdir(filepath); out.println(opmsg); %>

______________________________________________________

java中提供了io类库,可以轻松的用java实现对文件的各种操作。下面就来说一下如何用java来实现这些操作。 1。新建目录 <%@ page contentType="text/html;charset=gb2312"%> <% String filePath="c:/aaa/"; filePath=filePath.toString();//中文转换 java.io.File myFilePath=new java.io.File(filePath); if(!myFilePath.exists()) myFilePath.mkdir(); %> 2。新建文件 <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.io.*" %> <% String filePath="c:/哈哈.txt"; filePath=filePath.toString(); File myFilePath=new File(filePath); if(!myFilePath.exists()) myFilePath.createNewFile(); FileWriter resultFile=new FileWriter(myFilePath); PrintWriter myFile=new PrintWriter(resultFile); String strContent = "中文测试".toString(); myFile.println(strContent); resultFile.close(); %> 3。删除文件 <%@ page contentType="text/html;charset=gb2312"%> <% String filePath="c:/支出证明单.xls"; filePath=filePath.toString(); java.io.File myDelFile=new java.io.File(filePath); myDelFile.delete(); %> 4。文件拷贝 <%@ page contentType="text/html; charset=gb2312" %> <%@ page import="java.io.*" %> <% int bytesum=0; int byteread=0; file://读到流中 InputStream inStream=new FileInputStream("c:/aaa.doc"); FileOutputStream fs=new FileOutputStream( "d:/aaa.doc");byte[] buffer =new byte[1444]; int length; while ((byteread=inStream.read(buffer))!=-1) { out.println("

"+byteread+"
"); bytesum+=byteread; System.out.println(bytesum); fs.write(buffer,0,byteread); } inStream.close(); %> 5。整个文件夹拷贝 <%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.io.*" %> <%String url1="C:/aaa"; String url2="d:/java/"; (new File(url2)).mkdirs(); File[] file=(new File(url1)).listFiles(); for(int i=0;i 6。文件下载 <%@ page contentType="text/html; charset=gb2312" %> <%@ page import="java.io.*" %> <% String fileName = "zsc104.swf".toString(); //读到流中 InputStream inStream=new FileInputStream("c:/zsc104.swf"); //设置输出的格式 response.reset(); response.setContentType("bin"); response.addHeader("Content-Disposition","attachment; filename=/"" + fileName + "/""); //循环取出流中的数据 byte[] b = new byte[100]; int len; while((len=inStream.read(b)) >0) response.getOutputStream().write(b,0,len); inStream.close(); %> 7。数据库字段中的文件下载 <%@ page contentType="text/html; charset=gb2312" %> <%@ page import="java.sql.*"%> <%@ page import="java.lang.*" %> <%@ page import="java.io.*" %> <%@ page import="com.jspsmart.upload.*" %> <%@ page import="DBstep.iDBManager2000.*"%> <% int bytesum=0; int byteread=0; //打开数据库 ResultSet result=null; String Sql=null; PreparedStatement prestmt=null; DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000(); DbaObj.OpenConnection(); //取得数据库中的数据 Sql="select * from t_local_zhongzhuan "; result=DbaObj.ExecuteQuery(Sql); result.next(); file://将数据库中的数据读到流中 InputStream inStream=result.getBinaryStream("content"); FileOutputStream fs=new FileOutputStream( "c:/dffdsafd.doc"); byte[] buffer =new byte[1444]; int length; while ((byteread=inStream.read(buffer))!=-1) { out.println("
"+byteread+"
"); bytesum+=byteread; System.out.println(bytesum); fs.write(buffer,0,byteread); } %> 8。把网页保存成文件 <%@ page import="java.text.*"%> <%@ page import="java.util.*"%> <%@ page import="java.io.*"%> <%@ page import="java.net.*"%> <% URL stdURL = null; BufferedReader stdIn = null; PrintWriter stdOut = null; try { stdURL = new URL("http://www.163.com"); } catch (MalformedURLException e) { throw e; } try { stdIn = new BufferedReader(new InputStreamReader(stdURL.openStream())); stdOut = new PrintWriter(new BufferedWriter(new FileWriter("c:/163.html"))); } catch (IOException e) { } /***把URL指定的页面以流的形式读出,写成指定的文件***/ try { String strHtml = ""; while((strHtml = stdIn.readLine())!=null) { stdOut.println(strHtml); } } catch (IOException e) { throw e; } finally { try { if(stdIn != null) stdIn.close(); if(stdOut != null) stdOut.close(); } catch (Exception e) { System.out

——————————————————————————————

JAVA版本/** * 创建文件夹 * @param adir String 文件的路径文件名 */ public static void makeDir(String dir) { File d = new File(dir); if (!d.exists()) { d.mkdir(); } } /** * 拷贝一个目录下的文件(不包括目录本身)到另一个目录 * @param aoldDir String 要拷贝的目录 * @param anewDir String 拷贝到的目录 * @throws IOException */ public static void copyDir(String aoldDir, String anewDir) throws IOException { File oldDir = new File(aoldDir); File newDir = new File(anewDir); if (oldDir.isDirectory()) { if (!newDir.exists()) { newDir.mkdir(); } String[] children = oldDir.list(); for (int i = 0; i < children.length; i++) { copyDir(new File(oldDir, children[i]), new File(newDir, children[i])); } } else { copyFile(oldDir, newDir); } }

——————————————————————————————————————

<%@ page contentType="text/html; charset=gb2312" %><%@ page import="java.io.*;"%>目录创建示例<%/*writed by Yecon*///在F盘创建t1目录String mulu_t = request.getRealPath("/")+"UploadFile/album/";File file=new File(mulu_t+"目录名"); //声明 File 对象boolean result = false;//创建目录if(!file.exists()){result = file.mkdir();if(result){out.println("

成功创建目录F://t1.

");}}else{out.println("

目录F://t1已存在.

");}file = new File("F://t2//t3");if(!file.exists()){//创建目录,mkdir方法只能创建单个目录result = file.mkdir();if(result){out.println("

因为t2目录不存在,创建目录F://t2//t3失败.

");}}else{out.println("out.println

F://t2//t3已存在.

");}if(!file.exists()){//mkdirs方法可以创建多个目录result = file.mkdirs();if(result){out.println("");}}%>