日期:2014-05-18  浏览次数:20716 次

求上传图片源代码
因为要上传的图片很小,规格是90X120,所以想以字节流的形式直接存如数据库,然后在读出来的时候限制大小.     请高手以源代码相告!感激不尽!!!

------解决方案--------------------
楼上的让人想吐
------解决方案--------------------
看看这个怎么样?
public void WriteFile(String strFileName) {
try {
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
dbUrl = "jdbc:microsoft:sqlserver://192.168.0.88:1433;DatabaseName=lzegovnew;user=sa;password=sa; ";
con = DriverManager.getConnection(dbUrl);
state = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
sql = "select * from image ";
rs = state.executeQuery(sql);
//将image写入数据库中
String id=String.valueOf((int)(Math.random()*10000));
File myFile = new File(strFileName);
InputStream inword = new FileInputStream(myFile);
rs.moveToInsertRow(); // moves cursor to the insert row
rs.updateString( "id ", id); // updates the col:1
/**************文件的大小应该保存在一个字段中************/
rs.updateString( "size ", (文件的格式大小));
rs.updateBinaryStream( "image ", inword, (int) myFile.length());// updates// the// col:2,input// the// file 's// content
rs.insertRow();

//从数据库读出image
sql = "select * from image where id= ' "+id+ " ' and size= ' "+/*文件的大小格式*/+ " ' " ;
rs = state.executeQuery(sql);
rs.next();
OutputStream outword = new FileOutputStream(new File( "C:\\ "+id+ ".jpg "));
InputStream is = rs.getBinaryStream( "image ");// reads the file 's// content
int temp;

while ((temp = is.read()) != -1) {
outword.write(temp);
}
//关闭流对象和数据库对象
outword.close();
rs.close(); // closes the ResultSet
con.close();// closes the Connection
System.out.println( "Write to database successfully : ");
} catch (Exception e) {
e.printStackTrace();
}
}