久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Java Servlet 中上傳文件

Uploading a file in Java Servlet(在 Java Servlet 中上傳文件)
本文介紹了在 Java Servlet 中上傳文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 Java 動態 Web 項目,我正在使用 TomCat v7.0.

I have a Java Dynamic Web Project, and I'm using TomCat v7.0.

我是 web 項目的新手,我不太明白如何在我的一個 jsp 頁面中上傳文件.由于我的項目只是本地的,我想我可以使用一個多部分的形式,人們可以在其中選擇文件(這部分很好),然后從我的 Servlet 中檢索文件路徑.我無法完成這部分,它似乎只給了我文件的名稱,而不是它的整個路徑.

I am new to web projects and I didn't quite understand how I can upload a file in one of my jsp pages. Since my project is intended to be only local, I thought I could use a multipart form in which the person would choose the file (and this part goes fine) and later retreive the file path from my Servlet. I can't complete this part though, it appears to only give me the name of the file, not its entire path.

誰能指出我正確的方向?我已經閱讀了幾篇關于 Apache 文件上傳和從多部分表單中檢索信息的文章,但似乎沒有任何幫助.

Can anyone point me to the right direction? I've read several posts about Apache File Upload and retreiving information from the multipart form but nothing seems to help me.

如何從表單中獲取文件路徑,或者如何獲取上傳的文件以在我的 Java 類中使用?

How can I get the file path from a form or alternatively how can I get the uploaded file to use in my Java classes?

提前致謝.

.jsp:

<form method="post" action="upload" enctype="multipart/form-data">
<input type="file" name="filePath" accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"></input>
<input type="submit" value="Enviar"></input>
</form>

Java Servlet:

Java Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    PrintWriter out = response.getWriter();
    out.println("<html><body>");

    try
    {
        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        for (FileItem item : items)
        {
            if (item.isFormField())
            {
                // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                String fieldname = item.getFieldName();
                String fieldvalue = item.getString();

                out.println("<h1>"+fieldname+" / "+fieldvalue+"</h1>");
            }
            else
            {
                // Process form file field (input type="file").
                String fieldname = item.getFieldName();
                String filename = item.getName();
                InputStream filecontent = item.getInputStream();
                String s = filecontent.toString();
                out.println("<h1>"+s+" / "+filename+"</h1>");
                item.write(null);
            }
        }
    }
    catch (FileUploadException e)
    {
        throw new ServletException("Cannot parse multipart request.", e);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    out.println("</body></html>");
}

推薦答案

不提供文件路徑是瀏覽器的一項安全功能.

Not providing the file path is a security feature of the browser.

您的代碼中有可用的文件內容(InputStream filecontent),因此您可以使用它或使用 FileItem 上的一種便捷方法,例如

You have the file contents available in your code (InputStream filecontent) so you could use that or use one of the convenience methods on FileItem, e.g.

item.write(new File("/path/to/myfile.txt"));

這篇關于在 Java Servlet 中上傳文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产午夜三级一区二区三 | www.嫩草 | 97av视频在线观看 | 欧美一二三区 | 日韩毛片网 | 在线观看三级av | 日韩精品视频中文字幕 | 麻豆精品国产91久久久久久 | 国产精品一区二区三区在线 | 亚洲欧美综合精品久久成人 | 黄色片网站在线观看 | 99av成人精品国语自产拍 | 亚洲精品2区 | 亚洲欧美日韩精品久久亚洲区 | 中文字幕亚洲一区 | 婷婷在线视频 | 久久精品网 | 亚洲精品一区二区三区中文字幕 | 久久久久国产精品午夜一区 | 色www精品视频在线观看 | 欧美一级黄 | 精品久久久久久久久久久久久久 | 国产xxxx在线 | 国产欧美一区二区三区在线看 | 性色av一区 | 91精品国产色综合久久不卡98 | 日韩播放 | 国产精品久久久久久久久免费桃花 | 久久国产婷婷国产香蕉 | 日韩欧美在线视频 | 亚洲精品黄色 | 国内激情av片 | 日韩精品一区二区三区中文在线 | 欧美日韩中文在线 | 91免费视频观看 | 久久高清精品 | 国产美女在线精品免费 | 91久久北条麻妃一区二区三区 | 免费久草| 天天干免费视频 | 国产国产精品久久久久 |