struts2文檔上傳報錯

來源:互聯網
上載者:User

標籤:io   os   ar   java   for   sp   檔案   on   art   

            今天在寫文檔上傳的時候出現了INFO: Unable to find ‘struts.multipart.saveDir‘ property setting. Defaulting to javax.servlet.contex提示錯誤,後來尋找了寫資料發現是struts.xml中少了配置
 修改辦法為在struts2.xml中加入<constant name="struts.multipart.saveDir" value="/tmp"></constant>就ok了。如果實現文檔的多上傳,只要在前台jsp頁面多加幾個<input type="file" name="file">
 的表單。後台action類中加入  private static final String PATH="D:\\file\\";//檔案存放路徑
                                        private List<File> file;//上傳檔案數組
                                        private List<String> fileFileName;//檔案名稱,是檔案名稱的一個數組
                                        private String fileContentType;//記錄每個上傳檔案的類型,String類型
 此時file為input控制項中的name屬性(這個name值是自己起名字的比如我起的名字為file)通過如下代碼可以將上傳的文檔存放到檔案夾中:
                             if(upload!=null){
                                   //迴圈遍曆檔案
                                     for(int i=0;i<upload.size();i++){
                                 //取到檔案流
                                    InputStream is=new FileInputStream(upload.get(i));
                                 //建立檔案輸出資料流
                                    OutputStream os=new FileOutputStream(PATH+uploadFileName.get(i));
                                 //緩衝位元組
                                    byte buffer[]=new byte[1024];
                                 //緩衝位元組大小
                                   int count=0;
                                //寫檔案
                                   while((count=is.read(buffer))>0){
                                   os.write(buffer,0,count);
                                 }
                                    //關閉流
                                   os.close();
                                   is.close();
                             }
                       }

struts2文檔上傳報錯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.