`acc` mediumblob not null,————这样定义的,应该能存16M的东西才对
上传上去的东西不管是多少大小,下载下来以后都是40K。
上传一个一百多K的东西后下载下来就显示不正确了。
为什么啊?

解决方案 »

  1.   

    con = DriverManager.getConnection(url);
    in=new FileInputStream(this.apath + this.name);
    ps=con.prepareStatement("insert into accessory values(null,?,?,?)");
    ps.setInt(1,this.pid);
    ps.setString(2,this.name);
    ps.setBinaryStream(3, in, in.available());
    ps.executeUpdate();
    上传的代码是这样的。上传的东西小于40k下载出来都是完全正确的。
      

  2.   


    public class Accessory {
    int id;
    int pid;
    String acc;
    String name;
    String apath;

    public Connection Conn=null; // 数据库连接器
    public Statement Stmt=null; // 连接sql语句
    public PreparedStatement Pstmt=null; // 连接sql语句
    public ResultSet Rs=null; // 结果集

    String url="jdbc:mysql://localhost/proandans?user=root&password=root";

    Accessory(int pid,String name,String apath){
    this.pid = pid;
    this.name = name;
    this.apath = apath;
    }

    public boolean save() {
    boolean flag = false;
    Connection con = null;
    PreparedStatement ps=null;
    InputStream in=null; 
    try {
    con = DriverManager.getConnection(url);
    in=new FileInputStream(this.apath + this.name);
    ps=con.prepareStatement("insert into accessory values(null,?,?,?)");
    ps.setInt(1,this.pid);
    ps.setString(2,this.name);
    ps.setBinaryStream(3, in, in.available());
    ps.executeUpdate();
    flag = true;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    finally
    {
    try {
    //关闭流
    if(in!=null) in.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    //关闭相关连接
    try {
    con.close();
    ps.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    } return flag;
    }

    public boolean download(int id,String apath,String name){
    boolean flag = false;

    Connection con=null;   
            Statement st=null;   
            ResultSet rs=null;   
            InputStream in=null;   
            OutputStream out=null;   
      
            try {   
             con = DriverManager.getConnection(url);
                st=con.createStatement();   
                rs=st.executeQuery("select acc from accessory where id = " +id);   
                rs.next();  //将光标指向第一行   
                //从rs中读取stupic放进InputStream对象中   
                in=rs.getBinaryStream("acc");   
                //申明byte数组,用来存放图片流   
                byte[] b=new byte[40000];   
                in.read(b); //从InputStream对象中读取数据放进byte数组中   
                //实例化OutputStream对象,在D盘创建一个图片文件   
                out=new FileOutputStream(apath + name);   
                //将文件输出,内容则为byte数组里面的数据   
                out.write(b);   
                out.flush();  
                flag = true;
      
            } catch (SQLException e) {   
                // TODO Auto-generated catch block   
                e.printStackTrace();   
            }   
            catch (IOException e) {   
                // TODO Auto-generated catch block   
                e.printStackTrace();   
            }   
            finally  
            {   
                try {   
                    if(in!=null)   
                        in.close();   
                    if(out!=null)   
                        out.close();   
                } catch (IOException e) {   
                    // TODO Auto-generated catch block   
                    e.printStackTrace();   
                }   
                try {
    con.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }//关闭相关连接   
            }   
         
            return flag;
    }


    public static void main(String[] args) {
    // TODO Auto-generated method stub }}就是这个类了。
      

  3.   

    试了。改成16M,然后重启了mysql,没用
      

  4.   

    原来是代码的问题,在outputstream外面包了一层bufferedoutputstream之后就对了。