追忆~似水年华 2020-05-11 21:51 采纳率: 0%
浏览 666
已采纳

JSP获取图片,怎么通过Servlet上传图片到项目目录并将图片路径上传到Oracle数据库中?

写了一个,不知道为什么老是出错
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <title>Test</title>
    </head>
    <body>
        <h2>java web 上传图片到项目目录并将文件路径放到Oracle数据库</h2>
        <form method="post" name="form" action="<%= request.getContextPath()%>/InsertServlet" enctype="multipart/form-data" onsubmit="return checksubmit()" >
            <div class="form-group">
                <div class="field">
                    <a class="button input-file" href="javascript:void(0);">
                        <input name="photo" id="photo" size="100" type="file" data-validate="regexp#.+.(jpg|jpeg|png|gif)$:只能上传jpg|gif|png格式文件" onchange="upload(this)"/>
                    </a>
                </div>
                <div class="photo">
                    <img width="150" height="150" id="showPhoto" class="img-border radius" />
                </div>
            </div>
            <div class="form-button">
                <button class="button" type="submit">提交</button>
            </div>
        </form>

        <script type="text/javascript">
            var showPhoto = document.getElementById("showPhoto");
            var photo = document.getElementById("photo");
            function upload(obj) {
                showPhoto.src = window.URL.createObjectURL(obj.files[0]);
            }
            function checksubmit()
            {
                if (!photo.files[0]) {
                    return true;
                } else {
                    if (photo.files[0].size > 1024000) {
                        alert("图片尺寸大于1M,请重新选择");
                        return false;
                    }
                }
                return true;
            }
        </script>
    </body>
</html>


//图片类
import java.io.InputStream;
import java.io.Serializable;

public class Picture implements Serializable {

    private InputStream photo;
    private String photo_url;

    public Picture() {
    }

    public InputStream getPhoto() {
        return photo;
    }

    public void setPhoto(InputStream photo) {
        this.photo = photo;
    }

    public String getPhoto_url() {
        return photo_url;
    }

    public void setPhoto_url(String photo_url) {
        this.photo_url = photo_url;
    }

}


//访问Oracle数据库
    @Override
    public boolean insertPicture(Picture picture){
        try {
            conn = DatabaseBean.getConnection();
            if (picture.getPhoto().available() > 0) {
                String sql = "insert into picture(spicture,photo_url) values(?,?)";
                psmt = conn.prepareStatement(sql);
                psmt.setBinaryStream(1, picture.getPhoto(), picture.getPhoto().available());
                psmt.setString(2, picture.getPhoto_url());
            } else {
                String sql = "insert into picture(photo_url) values(?)";
                psmt = conn.prepareStatement(sql);
                psmt.setString(1, picture.getPhoto_url());
            }
            psmt.executeUpdate();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DatabaseBean.close(null, psmt, conn);
        }
        return false;
    }


        try {
            Part part = request.getPart("photo");
            long size = part.getSize();
            Picture picture = new Picture();
            picture.setPhoto(part.getInputStream());
            // 如果选择了上传图片,将图片保存到photo目录下,否则不保存
            if (size > 0) {
                String fileName = part.getSubmittedFileName();
                String savePath = request.getServletContext().getRealPath("/photo");
                part.write(savePath + File.separator + fileName);
                picture.setPhoto_url(fileName);
            } else {
                picture.setPhoto_url("nopic.png");
            }
            //存入新添加的图片信息记录
            boolean flag = DaoFactory.getPictureDao().insertPicture(picture);
            if (flag) {
                response.sendRedirect(request.getContextPath() + "/PictureDisplay");
            } else {
                request.getRequestDispatcher("insert.jsp").forward(request, response);
            }
        } catch (Exception ex) {
            request.getRequestDispatcher("insert.jsp").forward(request, response);
        }
  • 写回答

1条回答 默认 最新

  • dabocaiqq 2020-05-12 09:33
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料