六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 34|回复: 0

Strruts2+JQuery上传文件遇到的问题

[复制链接]

升级  92%

12

主题

12

主题

12

主题

童生

Rank: 1

积分
46
 楼主| 发表于 2013-1-23 02:44:29 | 显示全部楼层 |阅读模式
使用JQuery的ajaxfileupload上传文件:
js:
function ajaxFileUpload(fileName){if(!btnEnabler){alert("文件上传中,请稍候...");return;}btnEnabler = false;//load the loading image$("#fileLoading").ajaxStart(function(){$(this).show();}).ajaxComplete(function(){$(this).hide();});//file upload$.ajaxFileUpload({ url:'<%=path%>/email/uploadAttach.action?fileName='+escape(escape(fileName)), secureuri:false, fileElementId:'oFile', dataType: 'json', success: function (data, status){allFilePath=allFilePath + data.msg + "<*>";showFile();//clear the file selection dialog boxvar obj = document.getElementById("oFile");obj.outerHTML=obj.outerHTML;  btnEnabler = true;}, error: function (data, status, e){alert("文件上传失败");//clear the file selection dialog boxvar obj = document.getElementById("oFile");obj.outerHTML=obj.outerHTML;  btnEnabler = true;}})} 页面部分:
<input type="file" id="oFile" name="oFile" onchange="ajaxFileUpload(this.value);"><font id="fileLoading" style="display:none"><img src="<%=path%>/resource/image/Emailimage/loading.gif" border="0" align="absmiddle"> 正在上传附件...</font> 后台action:
private static final SimpleDateFormat fileNameSdf=new SimpleDateFormat("yyyyMMddHHmmss");  private File oFile; 提供oFile的get/set方法;
 
public void uploadAttach() throws EmailException {System.out.println("uploadAttach() invoke..." );HttpServletRequest request = ServletActionContext.getRequest();HttpServletResponse response = ServletActionContext.getResponse();    String uploadPath = EmailUtil.getUploadPath();    String fileNamePrefix = fileNameSdf.format(new Date());      String uploadFileName = Escape.unescape(request.getParameter("fileName"));        String storeFileName = uploadPath + fileNamePrefix + uploadFileName;        File storeFile = new File(storeFileName);           BufferedOutputStream bos = null;           BufferedInputStream bis = null;                  try {               bos = new BufferedOutputStream(new FileOutputStream(storeFile));               bis = new BufferedInputStream(new FileInputStream(oFile));               int c;               while ((c = bis.read()) != -1) {                   bos.write(c);                   bos.flush();               }               response.setContentType("text/html;charset=utf-8");            response.getWriter().write("{msg:'"+fileNamePrefix + uploadFileName+"'}");        } catch (IOException e) {               e.printStackTrace();               throw new EmailException("上传邮件附件出错!");        } finally {               try { bos.close(); } catch (IOException e) {e.printStackTrace();}            try { bis.close(); } catch (IOException e) {e.printStackTrace();}        }           } 困惑了半天的问题就是上传小于2M的文件没问题,但是上传大于2M的文件ajax返回error,action没有执行,找了半天是Struts2的default.properties配置文件对上传文件的限制为2M,要修改这个限制可以再struts.xml中修改这一常量
<constant name="struts.multipart.maxSize" value="20000000" /> 问题就搞定了!
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表