表单截取文件流

package servlet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.PreparedStatement;

import util.ConnectionDB;

public class FileUpload extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public FileUpload() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn=ConnectionDB.getConnection();
        FileInputStream str=null;
       
        int n;
        File f1=new File("D://temp.txt");
        try
        {
            InputStream is=request.getInputStream();
            BufferedInputStream bfis=new BufferedInputStream(is);
            FileOutputStream fout=new FileOutputStream(f1);
            BufferedOutputStream my_out=new BufferedOutputStream(fout);
            byte[] b=new byte[10000];
            while((n=bfis.read(b))!=-1)
            {
                 my_out.write(b,0,n);
            }
            my_out.flush();
            my_out.close();
            fout.close();
            bfis.close();
            is.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
       
       
        try{
            RandomAccessFile random1=new RandomAccessFile(f1,"r");//r是只读
            //读四行
            random1.readLine();//这一行是关于----------XXXXXXX这样的
            random1.readLine();
            random1.readLine();
            String filename=random1.readLine();
            //byte b[]=filename.getBytes("utf-8");
            byte b[]=filename.getBytes();
            filename=new String(b);
            System.out.println(filename);
           
            //读四行
            random1.readLine();
            String temp=random1.readLine();
            int endIndex=temp.lastIndexOf("/"");
            temp=temp.substring(0, endIndex);
            int startIndex=temp.lastIndexOf("/"");
            temp=temp.substring(startIndex+1, endIndex);
            System.out.println(temp);
           
            //用于定位文件的开始行和结尾行
            File f2=new File("D://"+temp);
            RandomAccessFile random2=new RandomAccessFile(f2,"rw");
            random1.seek(0);
            for(int i=0; i<8; i++)
            {
                 random1.readLine();
            }
            long startPoint=random1.getFilePointer();
            random1.seek(random1.length());
            long mark=random1.getFilePointer();
            int j=0;
            long endPoint=0;
            while((mark>=0)&&(j<=5))
            {
                 mark--;
                 random1.seek(mark);
                 n=random1.readByte();
                 if(n=='/n')
                 {
                       j++;
                       endPoint=random1.getFilePointer();
                 }
            }
            System.out.println(endPoint+"-"+startPoint);
            long length=endPoint-startPoint;
            System.out.println("中间的字节数量:"+length);
            random1.seek(startPoint);
            byte bt[]=new byte[1024];
            int count=(int)length/1024;//看一共要读几次
            int leftbyte=(int)length%1024;
            for(int i=0;i<count;i++){
                random1.read(bt);
                random2.write(bt);
            }
            random1.read(bt,0,leftbyte);
            random2.write(bt,0,leftbyte);
            str=new FileInputStream(f2);
            random1.close();
            random2.close();
            f1.delete();

        }catch(Exception ie){
            ie.printStackTrace();
        }
       
       
        try{
            Statement stat=conn.createStatement();
            try{
                stat.execute("CREATE TABLE TEST (Name VARCHAR(20),BlobFile LONGBLOB)");
            }catch(Exception ee){
                stat.execute("DROP TABLE TEST");
                stat.execute("CREATE TABLE TEST (Name VARCHAR(20),BlobFile LONGBLOB)");
            }
            String sql="insert into test(Name,BlobFile) values(?,?)";
            PreparedStatement pstat=(PreparedStatement) conn.prepareStatement(sql);
            pstat.setString(1,"lsh");
            pstat.setBinaryStream(2,str,str.available());//参数位置   输入流    长度
            pstat.execute();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                System.out.println("关闭连接失败");
                e.printStackTrace();
            }
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值