JSP实现图片上传并保存到数据库
采用servlet+jsp+mysql方式怎么写,尽量写上注释,谢谢,我试验成功后再加金币!谢谢大家!...
采用servlet+jsp+mysql 方式怎么写,尽量写上注释,谢谢,我试验成功后再加金币!谢谢大家!
展开
展开全部
servlet里面有一个request.getPart()方法,通过这个文件可以获得图片,前提是你的servlet版本必须是3.0以上+tomcat7,具体参考以下
@WebServlet("/articleManage")
@MultipartConfig(maxFileSize = 1024 * 1024 * 10)
// 最大10MB
public class ArticleManage extends HttpServlet {
private static final long serialVersionUID = 1L;
public ArticleManage() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
if (method != null) {
if (method.equals("add")) {
addArticle(request, response);
}
}
private void addArticle(HttpServletRequest request,
HttpServletResponse response) {
ArticleService service = new ArticleServiceImpl();
ArticleTypeService typeService=new ArticleTypeServiceImpl();
String content = null;
String email = null;
String tag = null;
String type=null;
try {
email = request.getParameter("email");
content = request.getParameter("content");
tag = request.getParameter("tag");
type=request.getParameter("type");
Part img = request.getPart("img");
String imgName = null;
if (img != null) {
// 设置文件路径,写到硬盘
String head = img.getHeader("content-disposition");
int index = head.lastIndexOf("=") + 2;
imgName = head.substring(index, head.length() - 1); // 上传文件时的文件名
imgName.lastIndexOf(".");
String suffix = imgName.substring(imgName.lastIndexOf(".")); // 文件后缀
if (!suffix.equals(".jpeg") && !suffix.equals(".jpg")
&& !suffix.equals(".png") && !suffix.equals(".gif")
&& !suffix.equals(".bmp")) {
System.out.println("innn******************");
// 非法文件
request.setAttribute("content", content);
request.setAttribute("email", email);
request.setAttribute("tag", tag);
request.setAttribute("type", type);
request.setAttribute("errorinfo",
"*您上传的文件不合法,只能上后缀为jpg,bmp,png,gif,jpeg的图片");
request.getRequestDispatcher("add.jsp").forward(request,
response); // 重新导航到表单页
return;
}
imgName = System.currentTimeMillis() + suffix;
img.write(this.getServletContext().getRealPath("/image/upload")
+ File.separator + imgName); // 写到硬盘
}
Article msg = new Article();
msg.setContent(content);
msg.setImg("image/upload/" + imgName);
msg.setEmail(email);
msg.setKeyWord(tag);
msg.setType(typeService.querySingle(Integer.parseInt(type)));
// 从session中取User
User user = (User) request.getSession().getAttribute("user");
msg.setUser(user);
service.addMsg(msg);
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
response.getWriter()
.write("<html !DOCTYPE html><body><div style='margin;auto;font-weight:bold;'><span style='font-size;17px;'>投稿成功</span>两秒后跳转到首页...</body></html>");
response.setHeader("refresh", "2;url=index.jsp");
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("content", content);
request.setAttribute("email", email);
request.setAttribute("tag", tag);
request.setAttribute("type", type);
request.setAttribute("errorinfo", "投稿失败,请检查上传文件的大小,不能大于10MB");
try {
request.getRequestDispatcher("add.jsp").forward(request,
response);
} catch (ServletException | IOException e1) {
e1.printStackTrace();
}
return;
} finally {
try {
service.closeConnResources();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
这是我前几天写的,有问题再问我
@WebServlet("/articleManage")
@MultipartConfig(maxFileSize = 1024 * 1024 * 10)
// 最大10MB
public class ArticleManage extends HttpServlet {
private static final long serialVersionUID = 1L;
public ArticleManage() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
if (method != null) {
if (method.equals("add")) {
addArticle(request, response);
}
}
private void addArticle(HttpServletRequest request,
HttpServletResponse response) {
ArticleService service = new ArticleServiceImpl();
ArticleTypeService typeService=new ArticleTypeServiceImpl();
String content = null;
String email = null;
String tag = null;
String type=null;
try {
email = request.getParameter("email");
content = request.getParameter("content");
tag = request.getParameter("tag");
type=request.getParameter("type");
Part img = request.getPart("img");
String imgName = null;
if (img != null) {
// 设置文件路径,写到硬盘
String head = img.getHeader("content-disposition");
int index = head.lastIndexOf("=") + 2;
imgName = head.substring(index, head.length() - 1); // 上传文件时的文件名
imgName.lastIndexOf(".");
String suffix = imgName.substring(imgName.lastIndexOf(".")); // 文件后缀
if (!suffix.equals(".jpeg") && !suffix.equals(".jpg")
&& !suffix.equals(".png") && !suffix.equals(".gif")
&& !suffix.equals(".bmp")) {
System.out.println("innn******************");
// 非法文件
request.setAttribute("content", content);
request.setAttribute("email", email);
request.setAttribute("tag", tag);
request.setAttribute("type", type);
request.setAttribute("errorinfo",
"*您上传的文件不合法,只能上后缀为jpg,bmp,png,gif,jpeg的图片");
request.getRequestDispatcher("add.jsp").forward(request,
response); // 重新导航到表单页
return;
}
imgName = System.currentTimeMillis() + suffix;
img.write(this.getServletContext().getRealPath("/image/upload")
+ File.separator + imgName); // 写到硬盘
}
Article msg = new Article();
msg.setContent(content);
msg.setImg("image/upload/" + imgName);
msg.setEmail(email);
msg.setKeyWord(tag);
msg.setType(typeService.querySingle(Integer.parseInt(type)));
// 从session中取User
User user = (User) request.getSession().getAttribute("user");
msg.setUser(user);
service.addMsg(msg);
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
response.getWriter()
.write("<html !DOCTYPE html><body><div style='margin;auto;font-weight:bold;'><span style='font-size;17px;'>投稿成功</span>两秒后跳转到首页...</body></html>");
response.setHeader("refresh", "2;url=index.jsp");
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("content", content);
request.setAttribute("email", email);
request.setAttribute("tag", tag);
request.setAttribute("type", type);
request.setAttribute("errorinfo", "投稿失败,请检查上传文件的大小,不能大于10MB");
try {
request.getRequestDispatcher("add.jsp").forward(request,
response);
} catch (ServletException | IOException e1) {
e1.printStackTrace();
}
return;
} finally {
try {
service.closeConnResources();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
这是我前几天写的,有问题再问我
展开全部
一般不直接存图片到数据库,一般存的是地址。在html页面里写<input file>标签,即可上传文件,取到文件的地址后存到数据库里,读取时读取地址即可
追问
十分感谢您回答,请问如何读取数据库的图片到jsp页面?!我的数据库有两个表,一个用户表(有这些字段,id、username、name、gender、password、age),一个图片信息表(有这些字段:id、picname、userid)
通过什么方法最好?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询