猿问

用upoadify插件在spring mvc 框架下上传文件,总是报错500(internal server error)求指导~

前端代码:

<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;

求指导,感激不尽!

慕盖茨4494581
浏览 649回答 11
11回答

跃然一笑

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){ 这样试试

忽然笑

后台报错有没有指向是哪一行?

智慧大石

后台没有报错

慕斯王

@桂蓉氏: 能帮忙看下我的代码问题在哪么?

aluckdog

@shayusuf: getBytes()换成getInputStream()呢

慕尼黑8549860

@桂蓉氏: 改过来之后,有报错

慕尼黑5688855

@shayusuf: 用这句呢 FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),file);

qq_笑_17

@桂蓉氏: 还是同样的错误呢 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.

慕姐8265434

@桂蓉氏: 您好,能看出问题出在哪么?

哆啦的时光机

@shayusuf: 类型转换错误,无法将RequestFacade转换为MultipartHttpServletRequest
随时随地看视频慕课网APP

相关分类

Java
我要回答