日期:2014-05-17  浏览次数:20696 次

jsp如何上传图片到数据库
问题很简单,我需要用一个jsp页面点击浏览图片选择图片后提交到Action。Action处理完成再跳到返回页面.
  图片存放好象先转换为二进制码在存的,我在网上找了好多 的但都是asp的,sql语句都是写在页面上的,不好
有,转换到jsp中的时候有很多的 错误。
现在求一个jsp的上传图片到数据库,求原代码。要正确的。分全给正确的人。

------解决方案--------------------
这个我也刚做一个,就是放到数据库里的呀!
我用的sqlserver呀!
在jsp里把图片选好了,提交到action里,
我不知道你提交的另一个jsp还是servlet里,不管,代码都是一样的

数据库连接自己写,我给你写主要的

//插入图片
public void insertImgToDb() {
try {
Connection con=this.getConDB();//连接
PreparedStatement stmt = con.prepareStatement("insert into images values(?)");
File file = new File("E:\\Image\\壁纸\\2078118_ximgnd071026222654.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 int savePic(){
try {
File file = new File("E:\\eclipsework\\Blog\\WebContent\\pic\\b1.jpg"); 
FileInputStream input = new FileInputStream(file);
String SQL = "insert into pics(pic) values(?)";
return jdbc.update(SQL, new Object[]{input.available()});
} catch (Exception e) {
System.out.println("存储失败");
return 0;
}
}
------解决方案--------------------
Java code


public class UpImgServlet extends HttpServlet ...{

    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("text/html");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        // org.apache.commons.fileupload.DiskFileUpload 是一个开源包里的。
        DiskFileUpload du = new DiskFileUpload();
        du.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
        du.setSizeThreshold(4096);// 设置缓存区大小 ,4 kb;
        // up 为 /WebRoot 下的 一个目录
        du.setRepositoryPath(request.getSession().getServletContext()
                .getRealPath("/up"));// 设置缓存目录

        // 得到所有文件
        try ...{
            List list = du.parseRequest(request);
            Iterator it = list.iterator();
            while (it.hasNext()) ...{
                org.apache.commons.fileupload.FileItem fileItem = (FileItem) it
                        .next();

                // 是否为表单元素。如文本框 等等。
                if (fileItem.isFormField()) ...{
                    String name = fileItem.getFieldName();
                
                    //通过流 用来读取表单元素里的内容。
                    java.io.BufferedReader br = new BufferedReader(
                               new InputStreamReader(fileItem.getInputStream()));
                    
                    //如果还有除文件域以外的其他表单元素 就用 if()进行名字一一匹配。
                    if(name.equals("description"))...{
                        String contents =  br.readLine();
                        System.out.println(contents);
                        
                    }
                    
                }
                // 文件域
                else ...{
                    // 获得文件名,这个文件名包括路径:
                    String fileName = fileItem.getName();
                    int index = fileName.lastIndexOf('.');
                    fileName = fileName.substring