亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JSP實現圖片上傳并保存到數據庫

JSP實現圖片上傳并保存到數據庫

qq_笑_17 2019-04-12 11:06:10
JSP實現圖片上傳并保存到數據庫
查看完整描述

3 回答

?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

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();
}
}

}



查看完整回答
反對 回復 2019-04-13
?
慕娘9325324

TA貢獻1783條經驗 獲得超4個贊

一般不直接存圖片到數據庫,一般存的是地址。在html頁面里寫<input file>標簽,即可上傳文件,取到文件的地址后存到數據庫里,讀取時讀取地址即可

查看完整回答
反對 回復 2019-04-13
  • 3 回答
  • 0 關注
  • 3853 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號