ssm使用ueditor怎么怎配置文件,以及上传图片到数据库后台代码

ssm使用ueditor怎么怎配置文件,以及上传图片到数据库后台代码,按网上方法配置文件总是出现错误

1个回答

很明显我们需要传递一个json串,只要把这些属性填好就行了!!!!!

废话不多说,直接上代码:

[html] view plain copy

@Controller  
public class ImageController {  

    @Autowired  
    private ImageService imageService;  


     @RequestMapping(value="/uploadImage",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)    
     @ResponseBody  
        public Map<String,Object> uploadFile(@RequestParam(value = "upfile", required = false) MultipartFile file,     
                HttpServletRequest request,HttpServletResponse response){   
         Map<String,Object> map = new HashMap<String, Object>();  
         String realName = null;  
         String uuidName = null;  
         String realPath = null;  

         try {  
             Image image  = new Image();  
             //文件原来的名称  
             realName = file.getOriginalFilename();  
             //得到这个文件的uuidname  
             uuidName = this.getUUIDFileName(file.getOriginalFilename());  
             //图片保存的工程  
             realPath = request.getServletContext().getRealPath("/images");  
             //真实路径  
             String roolPath = request.getSession().getServletContext().getRealPath("/");  
             image.setName(realName);  
             image.setUrl(roolPath);  
             image.setUuidname(uuidName);  

             //得到文件的输入流  
            InputStream in = new BufferedInputStream(file.getInputStream());  
            //获得文件的输出流  
            OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(realPath,uuidName)));  

            IOUtils.copy(in, out);  
            in.close();  
            out.close();  
            //将图片信息传递到我的数据库当中  
            int flag = imageService.insertImage(image);  

            if(flag!=0){  
             map.put("state", "SUCCESS");// UEDITOR的规则:不为SUCCESS则显示state的内容  
             map.put("url","/images/"+uuidName);         //能访问到你现在图片的路径  
             map.put("title","");  
             map.put("original","realName" );           
            }  
        } catch (IOException e) {  

        map.put("state", "文件上传失败!"); //在此处写上错误提示信息,这样当错误的时候就会显示此信息  
            map.put("url","");  
            map.put("title", "");  
            map.put("original", "");          
            e.printStackTrace();  
        }          
         return map;  
     }  


     //下面是我的两个方法,取的uuidname防止文件同名问题  
     private String getExtName(String s, char split) {    
           int i = s.lastIndexOf(split);    
           int leg = s.length();    
           return i > 0 ? (i + 1) == leg ? " " : s.substring(i+1, s.length()) : " ";    
       }    

     private String getUUIDFileName(String fileName){    
            UUID uuid = UUID.randomUUID();    
            StringBuilder sb = new StringBuilder(100);    
            sb.append(uuid.toString()).append(".").append(this.getExtName(fileName, '.'));    
            return sb.toString();    
        }  


}  
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐