当前位置: 技术问答>java相关
300分求解:图片上传/显示成功!但显示的形式不满意!
来源: 互联网 发布时间:2017-05-02
本文导语: 上传: String fname=request.getParameter("image"); fname=new String(fname.trim().getBytes("ISO8859_1"), "GBK"); String url=new String(); url="jdbc:microsoft:sqlserver://192.168.0.191:1433;DatabaseName=testdb"; Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); Connec...
上传:
String fname=request.getParameter("image");
fname=new String(fname.trim().getBytes("ISO8859_1"), "GBK");
String url=new String();
url="jdbc:microsoft:sqlserver://192.168.0.191:1433;DatabaseName=testdb";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection(url,"sa","123");
String sqlin="insert into AAAA (pic) values (?)";
File file = new File(fname);
long l1=file.length();
int l2=(int)l1;
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = con.prepareStatement(sqlin);
ps.setBinaryStream(1,fis,l2);
ps.executeUpdate();
ps.close();
fis.close();
显示
url="jdbc:microsoft:sqlserver://192.168.0.191:1433;DatabaseName=testdb";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection(url,"sa","123");
try{
Statement stmt = con.createStatement();
ResultSet rs1 = stmt.executeQuery("SELECT * FROM AAAA");
if (rs1.next())
{
byte [] blocco = rs1.getBytes("pic");
response.setContentType("image/jpg");
ServletOutputStream op = response.getOutputStream();
for(int i=0;i
String fname=request.getParameter("image");
fname=new String(fname.trim().getBytes("ISO8859_1"), "GBK");
String url=new String();
url="jdbc:microsoft:sqlserver://192.168.0.191:1433;DatabaseName=testdb";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection(url,"sa","123");
String sqlin="insert into AAAA (pic) values (?)";
File file = new File(fname);
long l1=file.length();
int l2=(int)l1;
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = con.prepareStatement(sqlin);
ps.setBinaryStream(1,fis,l2);
ps.executeUpdate();
ps.close();
fis.close();
显示
url="jdbc:microsoft:sqlserver://192.168.0.191:1433;DatabaseName=testdb";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection(url,"sa","123");
try{
Statement stmt = con.createStatement();
ResultSet rs1 = stmt.executeQuery("SELECT * FROM AAAA");
if (rs1.next())
{
byte [] blocco = rs1.getBytes("pic");
response.setContentType("image/jpg");
ServletOutputStream op = response.getOutputStream();
for(int i=0;i
|
用servlet写个输出,然后,这样就没问题了
|
用,showphoto就是你显示图片的文件