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

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
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Markdown在VScode编辑器下插入视频
  • ¥15 stm32c8t6工程,使用hal库
  • ¥100 有偿求易语言word文档取doc和docx页数方法或模块
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低
  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡