写了一个,不知道为什么老是出错
<%@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);
}