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

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

Java Web文件上傳失敗,不知哪兒出錯了

Java Web文件上傳失敗,不知哪兒出錯了

Helenr 2018-12-07 11:32:22
//數據庫連接 public class DBConnection { String driver="com.mysql.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/myweb"; Connection connection; Statement statement; ResultSet resultset; public DBConnection(){ try{ Class.forName(driver); connection=DriverManager.getConnection(url,"root","123456"); statement=connection.createStatement(); }catch(ClassNotFoundException |SQLException e){ e.printStackTrace(); } } public void openConnection(){ try{ Class.forName(driver); connection=DriverManager.getConnection(url,"root","123456"); statement=connection.createStatement(); System.out.println("數據庫連接成功"); }catch(ClassNotFoundException |SQLException e){ e.printStackTrace(); System.out.println("數據庫連接失敗"); } } public void closeConnection(){ if(statement!=null) try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(connection!=null) try { connection.close(); System.out.println("數據庫關閉成功"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("數據庫關閉失敗"); } } /*public static void main(String[] args) { new DBConnection().openConnection(); new DBConnection().closeConnection(); }*/ } //數據庫具體操作 public class BookOperate { private DBConnection dbConnection = new DBConnection(); private Statement statement = dbConnection.statement; private ResultSet resultset; public void DataBaseOpenConnection(){ dbConnection.openConnection(); } /*插入書籍*/ public int insertBook(String bookName,String bookIntroduce,String imgAddress){ String insertSql="insert into bookinf (bookName,bookIntroduce,bookImage) values('" +bookName+"','"+bookIntroduce+"','"+imgAddress+"')"; try{ dbConnection.openConnection(); int i=statement.executeUpdate(insertSql); //測試書籍信息添加 if(i>0){ System.out.println("書籍添加成功"); } else { System.out.println("書籍添加失敗"); } return i; }catch(SQLException e){ e.printStackTrace(); return -1; } } /*查詢書籍*/ public ResultSet selectBook(){ String selectSql="select * from bookinf"; try{ resultset=statement.executeQuery(selectSql); return resultset; }catch(SQLException e){ e.printStackTrace(); return null; } } public ResultSet selectBook(int bookId){ String selectSql="select * from bookinf where bookId="+bookId; try{ resultset=statement.executeQuery(selectSql); return resultset; }catch(SQLException e){ e.printStackTrace(); return null; } } public ArrayList<BookInf> selectBooks(){ ResultSet rs=this.selectBook(); BookInf bookInf=new BookInf(); ArrayList<BookInf> bookInfList=new ArrayList<BookInf>(); try{ while(rs.next()){ bookInf.setBookName(rs.getString(1)); bookInf.setBookIntroduce(rs.getString(2)); bookInf.setBookImage(rs.getString(3)); bookInfList.add(bookInf); } return bookInfList; }catch(SQLException e){ e.printStackTrace(); } return null; } public void DataBaseCloseConnection(){ dbConnection.closeConnection(); } } //Servlet /** * Servlet implementation class BookServlet */ @WebServlet("/BookServlet") public class BookServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public BookServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //圖片上傳后存儲在服務器端的目錄名稱 String savePath = this.getServletConfig().getServletContext() .getRealPath("/") + "uploads/"; System.out.println(savePath); //MultipartReques類主要是對文件上傳進行的處理,在上傳文件時,編碼格式為enctype="multipart/form-data"格式,以二進制形式提交數據,提交方式為post方式。 //第一個參數為傳過來的請求HttpServletRequest, //第二個參數為上傳文件要存儲在服務器端的目錄名稱 //第三個參數是用來限制用戶上傳文件大小 //第四個參數可以設定用何種編碼方式來上傳文件名稱,可以解決中文問題 MultipartRequest mul = new MultipartRequest(request, savePath, 10 * 1024 * 1024, "gbk"); request.setCharacterEncoding("gbk"); String bookName = mul.getParameter("bookName"); String bookIntroduce = mul.getParameter("bookIntroduce"); String imgAddress = null; //Enumeration是java.util中的一個接口類,在Enumeration中封裝了有關枚舉數據集合的方法。 //在Enumeration中提供了方法hasMoreElement()來判斷集合中是否還有其它元素和方法nextElement()來獲取下一個元素。利用這兩個方法可以依次獲得集合中元素。 //傳回所有文件輸入類型的名稱 Enumeration files = mul.getFileNames(); while (files.hasMoreElements()) { String fileName = (String) files.nextElement(); //用此方法得到上傳文件的真正的文件名,這里的fileName指文件輸入類型的名稱 String filedName = mul.getFilesystemName(fileName); imgAddress = "uploads/" + filedName;// 存在數據庫中的路徑 // imgAddress=fileName; System.out.println(fileName); System.out.println(filedName); } BookInf bookInf=new BookInf(); bookInf.setBookName(bookName); bookInf.setBookIntroduce(bookIntroduce); bookInf.setBookImage(imgAddress); BookOperate bookOperate=new BookOperate(); bookOperate.insertBook(bookName, bookIntroduce, imgAddress); ServletConfig config=getServletConfig(); ServletContext context=config.getServletContext(); RequestDispatcher dispatcher=context.getRequestDispatcher("/DisplayBook.jsp"); dispatcher.forward(request,response); } } //界面AddBook.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Add Book</title> </head> <body> <form action="AddBook" name="BookForm" method="post" enctype="multipart/form-data"> <table align=center width=600 border=1> <tr> <th colspan=2>添加書籍</th> </tr> <tr> <td>書籍名稱</td> <td><input type=text name=bookName size=90/></td> </tr> <tr> <td>書籍簡介</td> <td><textarea rows="10" cols="72" name=bookIntroduce></textarea></td> </tr> <tr> <td>書籍圖片</td> <td><input type=file name=bookImage/></td> <tr> <td align="center" colspan="2"><input type=submit value="提交"/></td> </tr> </table> </form> </body> </html> //界面DisplayBooks <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*,java.sql.*"%> <jsp:useBean id="bookInf" class="com.myweb.database.BookOperate" scope="session"/> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% int bookId = Integer.parseInt(request.getParameter("bookId")); ResultSet rs=bookInf.selectBook(bookId); if(rs.next()) { %> <p align="center"> <h1><%=rs.getString(2) %></h1> <font color="red"><%=rs.getString(3) %></font><br> <img src="<%=rs.getString(4)%>"/> </p> <% } %> </body> </html>
查看完整描述

3 回答

?
蝴蝶不菲

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

報錯信息貼一下,

查看完整回答
反對 回復 2018-12-16
?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

跟這些都沒有關系,弄了好幾天,終于出來了

查看完整回答
反對 回復 2018-12-16
  • 3 回答
  • 0 關注
  • 1130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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