jsp 에서 페이지 순 io 스 트림 업로드 파일 구현

5565 단어 파일 업로드
전송: 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 == '
') { // 。 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() == '
') j++; } // randomFile1 randomFile1.seek(forthEnterPosition); // randomFile1 startpoint long startPoint = randomFile1.getFilePointer(); // -1. '
', 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>

좋은 웹페이지 즐겨찾기