表單截取文件流

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();
            }
        }

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章