gpt4 book ai didi

java.io.FileNotFoundException : (No such file or directory) - Working with servlets 错误

转载 作者:行者123 更新时间:2023-11-28 22:29:54 25 4
gpt4 key购买 nike

我正在使用 hibernate 和 tomcat。我所做的是创建一个具有文件输入的表单。在 servlet 中,我想将该文件转换为 byte[]。但是当我想创建 de File 时,没有目录:它只保存图像的名称,而不是路径。这是错误:

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>

小服务程序:

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 {

}

}

最佳答案

希望您正在处理文件上传。

问题出在下面一行:

FileInputStream fileInputStream = new FileInputStream(image);

Servlet 在服务器上查找文件以读取它,但那时它不在那里。


使用Commons Fileupload library由 Apache 提供,可以轻松地向您的 servlet 和 Web 应用程序添加强大的、高性能的文件上传功能。

查找完整代码 hereherehere

关于java.io.FileNotFoundException : (No such file or directory) - Working with servlets 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24850125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com