关于Kindeditor上传照片不回显的问题

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/shifanzhang/article/details/76833783

项目中用到了Kindeditor,需要把图片上传到服务器上,然后并显示出来。以前一直显示不出来,一看路径是本地磁盘的路径这样是肯定显示不出来的,所以需要在后台把saveUrl修改一下就可以了。需要修改成相对路径,这样图片就能正常显示了

以下为Java代码:

public Object fileUpload(){
		String savePath=this.getCurSession().getServletContext().getRealPath("")+"/attached";
        //文件保存路径
		String saveUrl=this.getCurRequest().getContextPath()+"/attachedimage/";//相对路径
        //String saveUrl=this.getCurSession().getServletContext().getRealPath("")+"/attachedimage/";//绝对路径
        HttpServletResponse response=ServletActionContext.getResponse();
        PrintWriter out = null; //输出流 
        try {  
            out = response.getWriter();  
        } catch (IOException e1) {  
            e1.printStackTrace();  
        }  
        //定义能上传的文件格式
        HashMap<String, String> extMap=new HashMap<String, String>();
        extMap.put("image", "gif,jpg,jpeg,png,bmp");
        extMap.put("flash", "swf,flv");  
        extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");  
        extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,xml,txt,zip,rar,gz,bz2"); 
        //最大的文件大小
        long maxSize = 1000000;
        response.setContentType("text/html; charset=UTF-8");
        if(!ServletFileUpload.isMultipartContent(this.getCurRequest())){
        	return getError("请选择文件");
        }
        File uploadDir=new File(savePath);
        if(!uploadDir.isDirectory()){
        	uploadDir.mkdirs();
        	//return getError("上传目录不存在");
        }
        if(!uploadDir.canWrite()){
        	return getError("没有权利");
        }
        String dirName=this.getCurRequest().getParameter("dir");
        if(dirName==null){
        	dirName="image";
        }
        if(!extMap.containsKey(dirName)){
        	return getError("目录名不正确");
        }
        savePath+=dirName+"/";
        //saveUrl+=dirName+"/";
        File saveDirFile=new File(savePath);
        if(!saveDirFile.exists()){
        	saveDirFile.mkdirs();
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String ymd=sdf.format(new Date());
        savePath+=ymd+"/";
        saveUrl+=ymd+"/";
        File dirFile=new File(savePath);
        if(!dirFile.exists()){
        	dirFile.mkdirs();
        }
        FileItemFactory factory=new DiskFileItemFactory();
        ServletFileUpload upload=new ServletFileUpload(factory);
        upload.setHeaderEncoding("UTF-8");
        MultiPartRequestWrapper  wrapper = (MultiPartRequestWrapper) this.getCurRequest();
        String fileName=wrapper.getFileNames("imgFile")[0];
        File file=wrapper.getFiles("imgFile")[0];
        String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();  
        if (!Arrays.<String> asList(extMap.get(dirName).split(",")).contains(fileExt)) {  
            out.println(getError("上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(dirName)  
                    + "格式。"));  
            return null;  
        }
        if(file.length()>maxSize){
        	out.print(getError("上传文件大小超过限制"));
        	return null;
        }
        String newImgName = sdf.format(new Date()) + "_"  
                + new Random().nextInt(1000) + "." + fileExt;  
        byte[] buffer = new byte[1024];
        FileOutputStream fos;  
        // 获取内存中当前文件输入流  
        InputStream in;  
        try {  
            fos = new FileOutputStream(savePath + "/" + newImgName);  
            in = new FileInputStream(file);  
            int num = 0;  
            while ((num = in.read(buffer)) > 0) {  
                fos.write(buffer, 0, num);  
            }  
            in.close();  
            fos.close();  
        } catch (FileNotFoundException e1) {  
            e1.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } 
        com.alibaba.fastjson.JSONObject obj = new com.alibaba.fastjson.JSONObject();  
        obj.put("error", 0);  
        obj.put("url", saveUrl + "/" + newImgName);  
        out.println(obj.toJSONString());  
        return null;  
	}


展开阅读全文

没有更多推荐了,返回首页