文件上传,用通道,提高性能

if (request.getContentLength() > 297) {


request.setCharacterEncoding("UTF-8");  
//==================开始处理文件===================
System.out.println(request.getCharacterEncoding());
//接收上传文件内容中临时文件的文件名
        String tempFileName = new String("tempFileName.txt");
        //tempfile 对象指向临时文件
        File tempFile = new File(request.getRealPath("/")+tempFileName);
        //outputfile 文件输出流指向这个临时文件
        FileOutputStream outputStream = new FileOutputStream(tempFile);
        //得到客服端提交的所有数据
        InputStream fileSourcel = request.getInputStream();
        //将得到的客服端数据写入临时文件
        byte b[] = new byte[1000];
        int n ;
        while ((n=fileSourcel.read(b))!=-1){
            outputStream.write(b,0,n);
        }


        //关闭输出流和输入流
        outputStream.close();
        fileSourcel.close();


        //randomFile对象指向临时文件
        RandomAccessFile randomFile = new RandomAccessFile(tempFile,"r");
        //读取临时文件的前三行数据
        randomFile.readLine();
        randomFile.readLine();
        randomFile.readLine();
        //读取临时文件的第四行数据,这行数据中包含了文件的路径和文件名
        String filePath = randomFile.readLine();
        //得到文件名
        System.out.println(filePath);
        int position = filePath.lastIndexOf("filename");
        String filename =Tool.codeString(filePath.substring(position+10,filePath.length()-1));
        //重新定位读取文件指针到文件头
        randomFile.seek(0);
        //得到第四行回车符的位置,这是上传文件数据的开始位置
        long  forthEnterPosition = 0;
        int forth = 1;
        while((n=randomFile.readByte())!=-1&&(forth<=5)){
            if(n=='\n'){
                forthEnterPosition = randomFile.getFilePointer();
                forth++;
            }
        }


        //生成上传文件的目录
        File fileupLoad = new File(request.getRealPath("/"),"upLoad");
        fileupLoad.mkdir();
        //saveFile 对象指向要保存的文件
        File saveFile = new File(uploadPath,uuid+filename);
        RandomAccessFile randomAccessFile = new RandomAccessFile(saveFile,"rw");
        //找到上传文件数据的结束位置,即倒数第四行
        randomFile.seek(randomFile.length());
        long endPosition = randomFile.getFilePointer();
        System.out.println(endPosition);
        int j = 1;
        while((endPosition>=0)&&(j<=2)){
            endPosition--;
            randomFile.seek(endPosition);
            if(randomFile.readByte()=='\n'){
                j++;
            }
        }
        endPosition=endPosition-1;
        //从上传文件数据的开始位置到结束位置,把数据写入到要保存的文件中
        randomFile.seek(forthEnterPosition);
//         long startPoint = randomFile.getFilePointer();
//         while(startPoint<endPosition){
//             randomAccessFile.write(randomFile.readByte());
//             startPoint = randomFile.getFilePointer();
//         }
        long s = System.nanoTime();
        FileChannel fileChannelInput = randomFile.getChannel();
        FileChannel fileChannelOutput= new FileOutputStream(saveFile).getChannel();
        fileChannelInput.transferTo(0, fileChannelInput.size(), fileChannelOutput);
        fileChannelOutput.close();
        fileChannelInput.close();
    long d = System.nanoTime() - s;
        //关闭文件输入、输出
        randomAccessFile.close();
        randomFile.close();
        tempFile.delete();
//==================处理文件结束===================
//向控制台输出文件上传成功
System.out.println("File upload success!");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值