0 0

jquery的ajaxFileUpload插件的多参数上传后台获取到为null10

前台js  :

$.ajaxFileUpload({
    type: 'post',
    url:sysUrl+'/uploadFileServlet',
    dataType: 'json',
    data:{
            fileId:'fileId',
            fileType:'fileType',
            fileEnable:'fileEnable',
            ofilePath:'ofilePath',
            ozfilePath:'ozfilePath'
     },
    secureuri:false,
    fileElementId:'filePath',
    success: function (data) {
            alert("success");

    },error: function (data, status, e){
            alert("fail");

    }



后台 servlet  :

String fileId = request.getParameter("fileId");
String fileType = request.getParameter("fileType");
String fileEnable = request.getParameter("fileEnable");
String ofilePath = request.getParameter("ofilePath");
String ozfilePath = request.getParameter("ozfilePath");

打出来的所有对象都是 null   ,  在网上找的解决方案比如更改ajaxfileupload.js这个文件里面的几个地方,改了还是屁用没有,还浪费我下载积分。。谁遇到过这个问题,帮办忙呗
2014年7月27日 11:53

2个答案 按时间排序 按投票排序

0 0

String fileId = request.getParameter("fileId");
String fileType = request.getParameter("fileType");
String fileEnable = request.getParameter("fileEnable");
String ofilePath = request.getParameter("ofilePath");
String ozfilePath = request.getParameter("ozfilePath");

为什么要这样了?
既然前台是以json格式传过来 我先解析这个json 然后拿对应的值

2014年7月28日 02:31
0 0

用第三方fileupload组件,
后台代码
/**
* 上传文件入口
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestContext requestContext = new ServletRequestContext(request);
String saveFileName="";
String message="";
String result;
String basePath="";
String fileSize="0";
int fileLength=0;
boolean success=true;
if (FileUpload.isMultipartContent(requestContext)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");//设置编码,防止中文文件名乱码
upload.setSizeMax(Constants.FILE_UPLOAD_MAX_SIZE);
List items = new ArrayList();
try {
items = upload.parseRequest(request);
} catch (FileUploadException e1) {
success=false;
message="文件上传发生错误:" + e1.getMessage();
}

String randomUnid = (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date());

Iterator<?> it = items.iterator();
while (it.hasNext()) {
FileItem fileItem = (FileItem) it.next();
if (!fileItem.isFormField()) {
if (fileItem.getName() != null && fileItem.getSize() != 0) {
saveFileName=randomUnid+this.getExpandName(fileItem.getName());
basePath=Constants.getAppPath()+Constants.FILE_SEPARATOR+Constants.ATTACHMENT_PATH+Constants.FILE_SEPARATOR;
File attachmentDir=new File(basePath);
if(!attachmentDir.exists()){
attachmentDir.mkdir();
}
File newFile = new File(basePath+ saveFileName);
fileLength=fileItem.getInputStream().available();
fileSize=formetFileSize(fileLength);
try {
fileItem.write(newFile);
} catch (Exception e) {
success=false;
message="文件上传发生错误:" + e.getMessage();
e.printStackTrace();
}
} else {
success=false;
message="文件没有选择 或 文件内容为空";
}
}//end if

}//end while
}//end if

response.setHeader("Content-type", "text/html;charset=UTF-8");
        OutputStream out = response.getOutputStream();
    out.write("<meta http-equiv='content-type' content='text/html;charset=UTF-8'>".getBytes());
    result="{success:'"+success+"'," +
    "message:'"+message+"'," +
    "saveFileName:'"+Constants.getAppPath()+"temp/"+ saveFileName+"'," +
    "relativeFileName:'temp/"+ saveFileName+"'," +
    "fileSize:'"+fileSize+"',"+
    "fileLength:'"+fileLength+"'"+
    "}";
    out.write(result.getBytes());
    out.close();
}

2014年7月27日 21:54

相关推荐

Global site tag (gtag.js) - Google Analytics