jsp上传文件

本文介绍了一个使用Java和JSP实现的文件上传与处理流程。该流程包括客户端选择文件并提交到服务器,服务器端接收文件、解析文件内容并进行相应处理。文章详细展示了如何通过JSP页面实现文件的上传及服务器端的文件读取与处理逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新建 ex4_14.jsp
<%@ page contentType="text/html; charset=gb2312" language="java"
    import="java.util.*" pageEncoding="gb2312"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
    <%
      String str=response.encodeURL("acceptFile.jsp");
     %>
     <p>
选择要上传的文件 </p>
     <form action="<%=str %>" method="post" enctype="multipart/form-data">
     <input type=file name="boy" size="45">
     <br>
     <input type="submit" name="g" value="
提交 ">
     </form>
    
</body>
</html>

acceptFile.jsp

<%@ page contentType="text/html; charset=gb2312" language="java"
    import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.io.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
    <%
      String fileName=null;
      try{
          String tempFileName=(String)session.getId();
         
          File f1=new File("D:/test",tempFileName);
          FileOutputStream o=new FileOutputStream(f1);
         
          InputStream in=request.getInputStream();
          byte b[]=new byte[10000];
          int n;
          while((n=in.read())!=-1){
          o.write(b,0,n);
          }
          o.close();
          in.close();
         
          RandomAccessFile random=new RandomAccessFile(f1,"r");
         
          int second=1;
          String secondLine=null;
          while(second<=2){
             secondLine=random.readLine();
             second++;
           }
          //
获取第 2 行中目录符号 '/' 出现的位置
          int position=secondLine.lastIndexOf('//');
         
          fileName=secondLine.substring(position+1,secondLine.length()-1);
          random.seek(0);
         
          long forthEndPosition=0;
          int forth=1;
          while((n=random.readByte())!=-1&&(forth<=4)){
                  if(n=='/n'){
                     forthEndPosition=random.getFilePointer();
                     forth++;
                     }
          }
         
          byte cc[]=fileName.getBytes("ISO-8859-1");
          fileName=new String(cc);
          File f2=new File("D:/test",fileName);
          session.setAttribute("Name",fileName);
          RandomAccessFile random2=new RandomAccessFile(f2,"rw");
         
          random.seek(random.length());
         
          long endPosition=random.getFilePointer();
          long mark=endPosition;
          int j=1;
          while((mark>=0)&&(j<=6)){
                  mark--;
                  random.seek(mark);
                  n=random.readByte();
             
                  if(n=='/n'){
                     endPosition=random.getFilePointer();
                     j++;}
          }
         
          random.seek(forthEndPosition);
          long startPoint=random.getFilePointer();
          while(startPoint<endPosition-1)
          {
            n=random.readByte();
            random2.write(n);
            startPoint=random.getFilePointer();
          }
            random2.close();
            random.close();
            f1.delete();
             }
          catch(IOException ee){}
          out.print("
文件上传 ");
          out.print("fileName");

     %>
     <p>
查看效果 </p>
     <%
       String str=response.encodeURL("show.jsp");
      %>
     <form action="<%=str%>" >
     <input type="submit" value="
查看 ">
     </form>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值