第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

java io FileNotFoundException 異常

java io FileNotFoundException 異常

達令說 2022-11-30 13:43:51
我正在嘗試使用 JSP 將圖像上傳到 MySQL 數(shù)據(jù)庫。但它以找不到文件異常結(jié)束。我該如何解決?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 ReportMessage 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.
查看完整描述

1 回答

?
慕萊塢森

TA貢獻1810條經(jīng)驗 獲得超4個贊

request.getParameter() 方法將返回字符串,但要獲取實際圖像,您需要獲取文件部分輸入流。然后通過使用該部分,您可以在服務(wù)器上創(chuàng)建圖像,并進一步將其作為二進制文件存儲在數(shù)據(jù)庫中。最有效的方法是將所有文件存儲在外部存儲(如 S3 存儲桶)中,并將路徑存儲在數(shù)據(jù)庫中。你可以試試下面的代碼。


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")

@MultipartConfig

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");


        InputStream uploadedInputStream = null;

        Part filePart = null;

        String image = "";

        filePart = request.getPart("image"); // Retrieves <input type="file" name="image">

            if (filePart != null) {

                image = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); //image->name

                uploadedInputStream = filePart.getInputStream();

            }


        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);}

    }

}


查看完整回答
反對 回復(fù) 2022-11-30
  • 1 回答
  • 0 關(guān)注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號