学了 javaWeb后,做了一个上传资料的程序
使用SQLserver数据库,
刚开始把图片存进去了,就是取不出来,
请高手们教教 我这个新手啊
------解决方案--------------------
我来给你顶起,大家多帮帮忙啊
------解决方案--------------------
要用二进制的, 传进去前要转换byte 类型的
------解决方案--------------------
接触不多,不是很清楚
------解决方案--------------------
用DataSet的getBinaryStream()方法从数据库获得输入流
对流进行处理就行了
------解决方案--------------------
以下是获取数据库图片的方法,从表现层(一般是Servlet)传入,以下的三个参数,String name 为图片的ID用户名!
(此方法的作用是根据传入的用户名,查询对应用户的照片,然后输出!)
public void getImage(String name,HttpServletRequest request,
HttpServletResponse response) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
con = ds.getConnection();
st = con.createStatement();
rs = st.executeQuery("select P_photo from personnel where P_name='"+name+"'");
if (rs.next()) {
in = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/jpg"); //设置响应方式为图片形式
OutputStream toClient = null;
try {
toClient = response.getOutputStream();
byte[] P_Buf = new byte[len];
int i;
while ((i = in.read(P_Buf)) != -1) {
toClient.write(P_Buf, 0, i);
}
in.close();
toClient.flush();
toClient.close();
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
this.closeResultSet(rs); //因为采用Tomcat连接池技术,所以连接结束后关掉连接!
this.closeStatement(st);
this.closeConnection(con);
}
}
------解决方案--------------------
呵呵 昨天也有一个朋友问了这个问题
其实就是一个PreparedStatement类的应用。
把我昨天写的例子照拿过来
希望对你有帮助
- Java code
import java.io.*;import java.awt.image.BufferedImage;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.sql.*;public class imageServlet extends HttpServlet { /** * Constructor of the object. */ Connection conn = null; public imageServlet() { super(); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); this.conn = DriverManager.getConnection("jdbc:odbc:test"); } catch (Exception e) { e.printStackTrace(); } } public void insertImgToDb() { try { PreparedStatement stmt = this.conn.prepareStatement("insert into test (imgtest) values(?)"); File file = new File("d:\\a.JPG"); FileInputStream input = new FileInputStream(file); stmt.setBinaryStream(1, input,input.available()); //input.close(); stmt.executeUpdate(); stmt.close(); input.close(); } catch (Exception e) { e.printStackTrace(); } } public BufferedImage getImgFromDb() { BufferedImage image = null; try { PreparedStatement stmt = conn.prepareStatement("select imgtest from test"); ResultSet rs = stmt.executeQuery(); rs.next(); image = ImageIO.read(rs.getBinaryStream(1)); rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return image; } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg"); this.insertImgToDb(); BufferedImage img = this.getImgFromDb(); ImageIO.write(img, "JPG", response.getOutputStream()); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } public void init() throws ServletException { // Put your code here }}