java io FileNotFoundException 异常

我正在尝试使用 JSP 将图像上传到 MySQL 数据库。但它以找不到文件异常结束。我该如何解决?Servlet 文件、异常和表在下面...................................... ..................................................... ..................................................... ............ 表


package com.imageUpload.controller;


import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.sql.Connection;

import java.sql.PreparedStatement;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import com.imageUpload.utill.DbConnection;



@WebServlet("/ImageUpload")

public class ImageUpload extends HttpServlet {

    private static final long serialVersionUID = 1L;



    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String name=request.getParameter("name");

        String image=request.getParameter("image");


        FileInputStream fis=new FileInputStream(new File(image));


        Connection con=DbConnection.getConnection();

        try {   

        PreparedStatement ps=con.prepareStatement

            ("insert into image(name,image)values(?,?)");

        ps.setString(1,name);

        ps.setBinaryStream(3, fis);

        int n = ps.executeUpdate();

        if(n>0) {

            response.getWriter().println("Successfully Uploaded!");

        }


        }catch(Exception e) {System.out.println("Image E: "+e);}

    }



}


Type Exception Report


Message WS_Logo-02 (1).jpg (The system cannot find the file specified)


Description The server encountered an unexpected condition that prevented it from fulfilling the request.

达令说
浏览 101回答 1
1回答

慕莱坞森

request.getParameter() 方法将返回字符串,但要获取实际图像,您需要获取文件部分输入流。然后通过使用该部分,您可以在服务器上创建图像,并进一步将其作为二进制文件存储在数据库中。最有效的方法是将所有文件存储在外部存储(如 S3 存储桶)中,并将路径存储在数据库中。你可以试试下面的代码。package com.imageUpload.controller;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.imageUpload.utill.DbConnection;@WebServlet("/ImageUpload")@MultipartConfigpublic class ImageUpload extends HttpServlet {&nbsp; &nbsp; private static final long serialVersionUID = 1L;&nbsp; &nbsp; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; String name=request.getParameter("name");&nbsp; &nbsp; &nbsp; &nbsp; InputStream uploadedInputStream = null;&nbsp; &nbsp; &nbsp; &nbsp; Part filePart = null;&nbsp; &nbsp; &nbsp; &nbsp; String image = "";&nbsp; &nbsp; &nbsp; &nbsp; filePart = request.getPart("image"); // Retrieves <input type="file" name="image">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (filePart != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); //image->name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uploadedInputStream = filePart.getInputStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; FileInputStream fis=new FileInputStream(new File(image));&nbsp; &nbsp; &nbsp; &nbsp; Connection con=DbConnection.getConnection();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; PreparedStatement ps=con.prepareStatement&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ("insert into image(name,image)values(?,?)");&nbsp; &nbsp; &nbsp; &nbsp; ps.setString(1,name);&nbsp; &nbsp; &nbsp; &nbsp; ps.setBinaryStream(3, fis);&nbsp; &nbsp; &nbsp; &nbsp; int n = ps.executeUpdate();&nbsp; &nbsp; &nbsp; &nbsp; if(n>0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.getWriter().println("Successfully Uploaded!");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }catch(Exception e) {System.out.println("Image E: "+e);}&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java