JSP学习日记(二) 文件上传

来源:互联网 发布:python字典的get方法 编辑:程序博客网 时间:2024/06/10 13:56

uploadfile.jsp
============================================
<%@ page contentType="text/html;charset=utf-8" %>
<html>
<body>
 <p>选择要上传的文件:<br>
 <form action="uploadfile2.jsp" method="post" enctype="multipart/form-data">
 <!--这里必须使用post方式以及"multipart/form-data"-->
  文件:<input type=file name="inputfilefield" size="38"/><br>
  标题:<input type="text" name="title"/>   <br>
  <input type="submit" name="uploadbutton" value="upload..."/>
 </form>
 </p>
</body>
</html>
 
 
uploadfile2.jsp
============================================
<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import="java.io.*"%>
<html>
<body>
 <%
 try{
  InputStream in=request.getInputStream();
  File file=new File("G:/JspExercise/File", "uploaded.txt");
  FileOutputStream o=new FileOutputStream(file);
  byte[] b=new byte[1000];
  int n;
  while( (n=in.read(b)) != -1 ){
   o.write(b, 0, n);
  }
  o.close();
  in.close();  
 }
 catch(IOException e){
   e.printStackTrace();
 }
 out.println("文件已上传。");
 %>
 <BR>标题:<%=request.getParameter("title")%>
 <BR>由于采用multiType/form-data,不能通过request.getParameter()来获得参数
 <BR>这时,各种类型的参数都统一包含在inputstream中,因而只能<a href="uploadfile3.jsp">通过解析输入流来获得参数</a>
 
</body>
</html>
 
 
uploadfile3.jsp
============================================
<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import="java.io.*"%>
<html>
<body>
 <%--通过解析临时文件uploadFile来获得真正上传的内容--%>
 <%
 try{
  InputStream in=request.getInputStream();
  File dir = new File("G:/JspExercise/File");
  File f1=new File(dir, "uploaded.txt");
  RandomAccessFile random1=new RandomAccessFile(f1, "r");
  String formID = random1.readLine();
  if(formID == null){
   return ;
  }
  formID = formID.substring(formID.lastIndexOf('-')+1, formID.length());
  random1.seek(0);
  String line = random1.readLine(); 
  while(line != null && line.indexOf(formID) != -1 ){
   //处理某一个表单项
   line = random1.readLine();
   if(line != null ){
    if( line.indexOf("filename") != -1){
     //该表单项是上传的文件
     int pos = line.lastIndexOf("//");
     String fileName = line.substring(pos+1,line.length()-1);
     fileName = new String(fileName.getBytes("iso-8859-1"), "utf-8");
     File f2=new File(dir, fileName);
     RandomAccessFile random2=new RandomAccessFile(f2, "rw");
     random1.readLine();random1.readLine();          //文件指针下移两行
     long startPos = random1.getFilePointer();    //文件内容的起点
     long endPos = 0;     //文件内容的终点
     while(random1.readLine().indexOf(formID) == -1){//直到下一个表单项的起始行
      endPos = random1.getFilePointer() - 2;  //不包括回车行符
     }
     //重新创建文件
     random1.seek(startPos);
     int n;
     while(startPos < endPos){
      n=random1.readByte();
      random2.write(n);
      startPos = random1.getFilePointer();
     }
     random2.close();
     out.print("<br>上传文件:" + fileName );
     out.print("(<a href='"+ fileName +"'>查看</a>)");
     random1.seek(endPos + 2);                //指向下一个表单项的起始行
     line = random1.readLine();
    }
    else{
     //该表单项是输入的参数
     String paraName = line.substring(line.indexOf("/"")+1, line.length()-1);
     random1.readLine();              //文件指针下移一行
     StringBuffer buf = new StringBuffer();
     while( (line = random1.readLine()).indexOf(formID) == -1){
      buf.append(line);
     }
     String paraValue = new String(new String(buf).getBytes("iso-8859-1"), "utf-8");
     out.print("<br>输入参数:" + paraName + "=" + paraValue);
    }
   }
  }
  random1.close();
 }
 catch(IOException e){
   e.printStackTrace();
 }
 %>
 
</body>
</html>
原创粉丝点击