簡體   English   中英

JSP獲取上載文件的名稱

[英]JSP get the name of the uploaded file

我需要使用JSP將Blob文件上傳到MySQL數據庫。 我能夠做到這一點,但是我無法存儲實際文件的名稱。 這非常重要,因為下一步將是已上傳文件的列表。 這是我到目前為止所得到的:

@MultipartConfig
public class FileUploadServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        DBConnection DBC = new DBConnection();
// this is the database connection method
        Connection con = DBC.connection();

        InputStream inputFile = null;
        Part file = request.getPart("file");
        if(file != null){
            inputFile = file.getInputStream();
        }
        Date utilDate = new Date();
        Date sqlDate = new java.sql.Date(utilDate.getTime());

        try{
            String fileUpload = "insert into uploads(file,uploaded_date) values('"+inputFile+"','"+sqlDate+"')";
            Statement st = con.createStatement();
            int insertFile = st.executeUpdate(fileUpload);
            response.sendRedirect("ok.jsp");
        }catch(SQLException e){

        }
    }

因此,如果我上傳一個名為picture.jpg的文件,我想將picture.jpg存儲在一個String中,而不是流java.io.FileInputStream@55c3ece1之類的東西。 提前致謝!

根據Java EE教程 ,可以從Content-Disposition Header中檢索文件名。 鏈接的文章顯示了如何做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM