【ZT】Jsp/Servlet:实现文件上传与下载【二】

12 篇文章 0 订阅
4.测试
fileupload.jsp不变,accept.jsp修改如下:
<html>
<head>
<%@ page language="java" import="java.io.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>The real file</title>
</head>
<body>
<%try{
    //use sessionid to create a temp file.
    String tempFileName=(String)session.getId();
    //create the temp file.
    File temp=new File("d:/temp",tempFileName);
    FileOutputStream o=new FileOutputStream(temp);
    if(request.getContentLength()>297){
      //write the upload content to the temp file.
      InputStream in=request.getInputStream();
      byte b[]=new byte[1024];
      int n;
      while((n=in.read(b))!=-1){
         o.write(b,0,n);
      }
      o.close();
      in.close();
      //read the temp file.
      RandomAccessFile random=new RandomAccessFile(temp,"r");
      //read Line2 to find the name of the upload file.
      int second=1;
      String secondLine=null;
      while(second<=2){
         secondLine=random.readLine();
         second++;
      }
      //get the last location of the dir char.'\\'.
      int position=secondLine.lastIndexOf('\\');
      //get the name of the upload file.
      String fileName=secondLine.substring(position+1,secondLine.length()-1);
      //relocate to the head of file.
      random.seek(0);
      //get the location of the char.'Enter' in Line4.
      long forthEndPosition=0;
      int forth=1;
      while((n=random.readByte())!=-1&&(forth<=4)){
         if(n=='\n'){
             forthEndPosition=random.getFilePointer();
             forth++;
         }
      }
      File realFile=new File("d:/temp",fileName);
      RandomAccessFile random2=new RandomAccessFile(realFile,"rw");
      //locate the end position of the content.Count backwards 6 lines.
      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++;
         }
      }
      //locate to the begin of content.Count for 4 lines's end position.
      random.seek(forthEndPosition);
      long startPoint=random.getFilePointer();
      //read the real content and write it to the realFile.
      while(startPoint<endPosition-1){
         n=random.readByte();
         random2.write(n);
         startPoint=random.getFilePointer();
      }
      random2.close();
      random.close();
      //delete the temp file.
      temp.delete();
      out.print("File upload success!");
    }
    else{
       out.print("No file!");
    }
}
catch(IOException e){
    out.print("upload error.");
    e.printStackTrace();
}
%>
</body>
</html>
(注:如果文件名是中文,会出现乱码。)
5.文件下载
Jsp内置对象response调用方法getOutputStream()可以获取一个指向客户的输出流,服务器将文件写入这个流,然后可下载此文件。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值