首页 > 代码库 > struts2文档上传报错

struts2文档上传报错

            今天在写文档上传的时候出现了INFO: Unable to find ‘struts.multipart.saveDir‘ property setting. Defaulting to javax.servlet.contex提示错误,后来查找了写资料发现是struts.xml中少了配置
 修改办法为在struts2.xml中加入<constant name="struts.multipart.saveDir" value="http://www.mamicode.com/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文档上传报错