我正在使用hibernate和tomcat。我所做的是創建一個具有文件輸入的表單。在servlet中,我想將該文件轉換爲byte []。但是當我想創建de File時,沒有目錄:它只保存圖像的名稱,而不是路徑。以下是錯誤:java.io.FileNotFoundException:(沒有這樣的文件或目錄) - 使用servlets
java.io.FileNotFoundException: oldi.png (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:138) at control.servlets.AddGenericItem.doPost(AddGenericItem.java:46) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:724)
的HTML:
<tr>
<td>
<font class="text_title">Image: </font>
</td>
<td>
<input name="image" type="file">
</td>
</tr>
<tr>
<td>
<input type="submit" value="submit" name="ctl00$MainContent$RegisterUserWizard$CreateUserStepContainer$CreateButton"
id="ctl00_MainContent_RegisterUserWizard_CreateUserStepContainer_CreateButton"
style="border-width:0px;">
</td>
</tr>
的Servlet:
public class AddGenericItem extends HttpServlet {
@Override
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException {
// HttpSession session = httpServletRequest.getSession();
DatabaseManager databaseManager = new DatabaseManager();
String name = httpServletRequest.getParameter("name");
String description = httpServletRequest.getParameter("description");
float price = Float.parseFloat(httpServletRequest.getParameter("price"));
File image = new File(httpServletRequest.getParameter("image"));
byte[] imageArray = new byte[(int) image.length()];
try {
FileInputStream fileInputStream = new FileInputStream(image);
fileInputStream.read(imageArray);
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Admin admin = (Admin) httpServletRequest.getSession().getAttribute("user");
Brand brand = admin.getBrand();
GenericItem genericItem = new GenericItem(name, description, imageArray, price,brand);
GenericItemDAO genericItemDAO = databaseManager.getGenericItemDAO();
genericItemDAO.beginTransaction();
genericItemDAO.save(genericItem);
try {
genericItemDAO.commitTransaction();
} catch (DatabaseAccessFailException e) {
e.getStackTrace();
} finally {
httpServletResponse.sendRedirect("/adminPages/items.jsp");
}
}
@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException {
}
@Override
protected void doDelete(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException {
}
}
哪裏是'oldi.png'項目中存在的? – Braj
你想上傳圖片嗎? – iMBMT
oldi.png在項目中不存在,我試圖從桌面上載它。 – user3808750