jsp中实现一个页面纯io流上传文件

转载:http://ptdxcd.blog.163.com/blog/static/85494827200810125211580/

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@page import="java.io.*"%>
<%@page import="java.sql.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'Xs.jsp' starting page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
  
  <body> 
  
     <%!
   //此方法用于将指定的文本转换成ISO-8859-1字符编码格式
   public String codeToString(String str) {
       String s = str;
       try {
         byte tempB[] = s.getBytes("ISO-8859-1");
         s = new String(tempB);
         return s;
       }
       catch (Exception e) {
         return s;
       }
   }
   %>
   
   <%
   //声明一个临时文件名
   String tempFileName = new String("tempFileName1");
   //在D盘建立一个临时文件,名为tempFileName
   File tempFile1 = new File("D:/", tempFileName);
   //根据指定的这个临时文件,来创建一个文件字节输出流(用于向磁盘写文件)
   FileOutputStream outputFile1 = new FileOutputStream(tempFile1);
   //由返回一个文件字节输入流(用于读文件)
   InputStream fileSource1 = request.getInputStream();
   //实例一个字节数组初始为1000kb
   byte b[] = new byte[1000];
   int n;
   //利用while循环,先读出这个文件大小,判断为不为-1,如果不为-1就输出数据,向磁盘写文件数据
   while ((n = fileSource1.read(b)) != -1) {
       outputFile1.write(b, 0, n); //将 n 个字节从b字节数组写入到outputFile1文件,偏移量从0开始
   }
   //关闭文件输出流
   outputFile1.close();
   //关闭文件输入流
   fileSource1.close();
   //实例化一个RandomAccessFile 对象.创建从中读取和向其中写入(可选)的随机存取文件流,该文件由 File 参数指定。
   //将创建一个新的 FileDescriptor 对象来表示此文件的连接。
   //mode 参数指定用以打开文件的访问模式。允许的值及其含意为:
   //"r" 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。
   //"rw" 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。
   //"rws" 打开以便读取和写入,对于 "rw",还要求对文件的内容或元数据的每个更新都同步写入到基础存储设备。
   //"rwd"   打开以便读取和写入,对于 "rw",还要求对文件内容的每个更新都同步写入到基础存储设备。
   RandomAccessFile randomFile1 = new RandomAccessFile(tempFile1, "r");
   //读取文本的下一行
   randomFile1.readLine();
   //读取文本的下一行返回一个String
   String FilePath = randomFile1.readLine();
   //对页面首次加载进行处理
   if(FilePath == null)
   {
    FilePath = "";
   }else
   {
   //得到最后一次出现\\符号的索引位置
   int position = FilePath.lastIndexOf('\\');
   //将截取到的文本(position + 1, FilePath.length() - 1)转换成ISO-8859-1格式的字符串(得到文件的名字)
   String filename = codeToString(FilePath.substring(position + 1, FilePath.length() - 1));
   //设置randomFile1文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。
   randomFile1.seek(0);
   //设置一个向前键入的索引位置
   long forthEnterPosition = 0;
   //设置一个向前的索引位置
   int forth = 1;
   while ((n = randomFile1.readByte()) != -1 && (forth <= 4))
       if (n == '\n') {
         //向前键入的位置为文件中的当前偏移量。
         forthEnterPosition = randomFile1.getFilePointer();
         forth++;
       }
   //由给定的虚拟路径返回一个真正的路径(这个虚拟路径由ServletContext.getRealPath("/")方法得到)相当于新建一个文件夹
   File FileUploadDir = new File(request.getSession().getServletContext().getRealPath("/"), "upLoadFile");
   //创建此抽象路径名指定的目录。
   FileUploadDir.mkdir();
   //在这个路径下新建一个文件,文件名就是上面得到的文件名
   File saveFile1 = new File(request.getSession().getServletContext().getRealPath("/") + "upLoadFile/", filename);
   //创建从中读取和向其中写入(可选)的随机存取文件流,访问模式为 可读可写
   RandomAccessFile randomFile2 = new RandomAccessFile(saveFile1, "rw");
   //设置randomFile2文件的偏移量为randomFile1的长度
   randomFile1.seek(randomFile1.length());
   //得到randomFile1的文件偏移量赋给endPosition
   long endPosition = randomFile1.getFilePointer();
   int j = 1;
   while ((endPosition >= 0) && (j <= 4)) {
       endPosition--;
       randomFile1.seek(endPosition);
       if (randomFile1.readByte() == '\n')
         j++;
   }
   //重新设置randomFile1的文件偏移量
   randomFile1.seek(forthEnterPosition);
   //得到randomFile1的文件偏移量赋给startpoint
   long startPoint = randomFile1.getFilePointer();
   //如果开始索引处小于最后的索引处-1.因为最后一个是'\n',
   while (startPoint < endPosition - 1) {
       //就给randomFile2文件写入randomFile1的所有字节数据..就相当于复制
       randomFile2.write(randomFile1.readByte());
       //重新设置开始索引为randomFile1的文件偏移量
       startPoint = randomFile1.getFilePointer();
   }
   //关闭randomFile2
   randomFile2.close();
   //关闭randomFile1
   randomFile1.close();
   //删除临时文件
   tempFile1.delete();
   //提示xx文件上传成功
   out.print("文件:" + filename + " 上传成功!<br>");
   }
   %>
    
    <form action="Xs.jsp" method=post enctype="multipart/form-data">
     <input type="file" name="upfile" size="30">
     <br>
     <input type="submit" name="submit" value="commit">
    </form>
  </body>
</html>

 

posted @ 2013-03-01 11:41  古来征战几人回  阅读(450)  评论(0编辑  收藏  举报