国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

file批量上傳文件,upload.parseRequest(request)返回空

2019-11-10 19:53:26
字體:
供稿:網(wǎng)友

1、用 file標(biāo)簽實(shí)現(xiàn),加上multiple屬性可以多選,但是IE瀏覽器不支持該屬性

      頁面代碼:

<form action="uploadfile.action" method="post" enctype="multipart/form-data">         <table>               <tr>               <td><input type="file" id ="file" name="file" multiple="multiple" size="40"></td>             </tr>               <tr>               <td colspan="2" align="center"><input type="submit" value="上傳"/></td>             </tr>           </table></form>

       后臺(tái)處理:

      items = upload.parseRequest(request);返回空值,解決方法分為3步

1)、增加一個(gè)類

public class MyRequestParser extends JakartaMultiPartRequest {public void parse(HttpServletRequest  servletRequest,   String   saveDir) throws IOException {} }

2)、修改struts.xml

<struts>    <!-- struts2.3.4版本 --><!--     <bean type= "org.apache.struts2.dispatcher.multipart.MultiPartRequest"           name= "myRequestParser" class= "actiontest.MyRequestParser"           scope= "default" optional= "true " />     <constant name= "struts.multipart.handler" value= "myRequestParser" /> -->        <!-- struts2.3.15.1 以后的版本 -->    <bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest"             name="myMultiPartRequest" class="actiontest.MyRequestParser"             scope="default" optional="true"/>      <constant name="struts.multipart.parser" value="myMultiPartRequest" /> 

3、

    public String uploadFiles() throws IOException {    request.setCharacterEncoding("UTF-8");     PRintWriter out = null;        FileItemFactory factory = new DiskFileItemFactory();        ServletFileUpload upload = new ServletFileUpload(factory);        File directory = null;        List<FileItem> items = new ArrayList();        try {

            //該句代碼在執(zhí)行時(shí)返回的items為空            items = upload.parseRequest(request);            // 得到所有的文件            Iterator<FileItem> it = items.iterator();            while (it.hasNext()) {                FileItem fItem = (FileItem) it.next();                String fName = "";                Object fValue = null;                if (fItem.isFormField()) { // 普通文本框的值                    fName = fItem.getFieldName();                    fValue = fItem.getString("UTF-8");                } else { // 獲取上傳文件的值                        fName = fItem.getFieldName();                        fValue = fItem.getInputStream();                        String name = fItem.getName();                        if(name != null && !("".equals(name))) {                            name = name.substring(name.lastIndexOf(File.separator) + 1);                                                        directory = new File("d://test");                            directory.mkdirs();                                String filePath = ("d://test")+ File.separator + name;                                                        InputStream is = fItem.getInputStream();                            FileOutputStream fos = new FileOutputStream(filePath);                            byte[] buffer = new byte[1024];                            while (is.read(buffer) > 0) {                                    fos.write(buffer, 0, buffer.length);                            }                            fos.flush();                            fos.close();                            }                        }                    }                } catch (Exception e) {                return "error";                }        return "success";      }

      


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 潞西市| 华池县| 达孜县| 江北区| 怀化市| 平果县| 金沙县| 永登县| 正安县| 荔浦县| 安康市| 塔河县| 德江县| 江孜县| 淮北市| 应城市| 江油市| 修文县| 会东县| 广东省| 高唐县| 闽侯县| 抚远县| 邯郸县| 百色市| 周至县| 新乡县| 霍州市| 扶绥县| 鞍山市| 高邮市| 宿州市| 和林格尔县| 曲阜市| 藁城市| 榆林市| 吉安市| 伊宁县| 鲁山县| 交口县| 水城县|