前端代码:
<link rel="stylesheet" href="uploadify/uploadify.css"type="text/css">
<script src="assets/js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="uploadify/swfobject.js"></script>
<script type="text/javascript" src="uploadify/jquery.uploadify.js"></script>
$(document).ready(function(){
$("#mediaID01").uploadify({
'swf' : 'uploadify/uploadify.swf',
'uploader' : '../WxController/saveMediaInServer',//后台处理的请求
'cancelImg' : 'img/uploadify-cancel.png',
'folder' : 'newWxQyServer/img/',//您想将文件保存到的路径
'queueID' : 'fileQueue',//与下面的id对应
'queueSizeLimit' : 5,
'fileDesc' : 'Image Files (*.jpg;*.jpeg;*.gif;*.png)',
'fileExt' : '*.jpg;*.jpeg;*.gif;*.png', //控制可上传文件的扩展名,启用本项时需同时声明fileDesc
'auto' : false,
'multi' : true,
'simUploadLimit' : 2,
'buttonText' : '选择图片',
'buttonImg':'',
'method':'post',
'removeCompleted':false,});
<div id="fileQueue"></div>
<input type="file" name="uploadify" id="mediaID01" />
<p>
<a href="javascript:jQuery('#mediaID01').uploadify('upload','*');">上传</a>
</p>
后台代码:
@RequestMapping(value = "/saveMediaInServer",method = RequestMethod.POST)
@ResponseBody
public List<String> saveMediaInServer(HttpServletRequest request, HttpServletResponse response,@RequestParam(value = "formData", required = false) String formData){
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
List<String> responseStr= new ArrayList<String>();
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
// 创建文件夹
Map<String, Object> type = JSON.parseObject(formData, new TypeReference<Map<String, Object>>(){});
Date now=new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
String filePath = "/upload/"+type.get("typeName")+"/"+dateFormat.format(now);
String path=request.getSession().getServletContext().getRealPath(filePath);
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
// 上传文件
MultipartFile mf = entity.getValue();
String filename=String.valueOf(System.currentTimeMillis()) + (int)(Math.random()*1000000);
File uploadFile = new File(path + filename);
try {
FileCopyUtils.copy(mf.getBytes(), uploadFile);
responseStr.add("上传成功");
} catch (IOException e) {
responseStr.add("上传失败");
e.printStackTrace();
}
}
return responseStr;
求指导,感激不尽!
public List<String> saveMediaInServer(HttpServletRequest request, HttpServletResponse response,@RequestParam(value = "formData", required = false) String formData){
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
改成
public List<String> saveMediaInServer(MultipartHttpServletRequest multipartRequest, HttpServletResponse response){
这样试试
后台报错有没有指向是哪一行?
后台没有报错
@shayusuf: 给你参考下
String tempName=new Date().getTime()+""+new Random().nextInt(100);
String tempDirPath = request.getSession().getServletContext().getRealPath(this.TEMPPATH);
if(!CommonFunction.isDirExist(tempDirPath))
CommonFunction.createDir(tempDirPath);
MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
/** 页面控件的文件流**/
MultipartFile multipartFile = mRequest.getFile("file");
/** 获取文件的后缀* */
String realName= multipartFile.getOriginalFilename();
String suffix="";
if(realName.lastIndexOf(".")>0)
suffix=realName.substring(realName.lastIndexOf("."));
String filePath = tempDirPath + File.separator + tempName + suffix;
File file = new File(filePath);
//multipartFile.transferTo(file);
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),file);
if (CommonFunction.isExistFile(filePath)){
result = file.getName();
}
@桂蓉氏: 能帮忙看下我的代码问题在哪么?
@shayusuf: getBytes()换成getInputStream()呢
@桂蓉氏: 改过来之后,有报错
@shayusuf: 用这句呢
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),file);
@桂蓉氏: 还是同样的错误呢
TTP Status 500 - Request processing failed; nested exception is java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest
type Exception report
message Request processing failed; nested exception is java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest
description The server encountered an internal error that prevented it from fulfilling this request.
@桂蓉氏: 您好,能看出问题出在哪么?
@shayusuf: 类型转换错误,无法将RequestFacade转换为MultipartHttpServletRequest