我正在使用hibernate和tomcat。我所做的是创建一个具有文件输入的表单。在servlet中我想将该文件转换为byte []。但是当我想创建de File时,没有目录:它只保存图像的名称,而不是路径。这是错误:
java.io.FileNotFoundException:oldi.png(没有这样的文件或目录) 在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 at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)at at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009) 在 org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:589) 在 org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:310) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615) 在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 {
}
}
答案 0 :(得分:0)
我希望您正在处理文件上传。
问题出在以下几行:
FileInputStream fileInputStream = new FileInputStream(image);
Servlet在服务器上查找文件以读取它,但那时它不会在那里。
使用Apache提供的Commons Fileupload library,可以轻松地为您的servlet和Web应用程序添加强大,高性能的文件上载功能。