Home  >  Article  >  Backend Development  >  The problem of unable to upload images when integrating kindeditor and Struts2 framework_PHP tutorial

The problem of unable to upload images when integrating kindeditor and Struts2 framework_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:53:52879browse

Since the struts framework encapsulates the request object, the original upload_json.jsp file that processes uploaded images cannot be used, so the upload_json.jsp file that processes uploaded images in kindeditor was rewritten. After passing many tests , successfully uploaded the image.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <%@ page import="java.util .*,java.io.*"%>
  <%@ page import="java.text.SimpleDateFormat"%>
  <%@ page import="org.apache.commons.fileupload .*"%>
  <%@ page import="org.apache.commons.fileupload.disk.*"%>
  <%@ page import="org.apache.commons.fileupload .servlet.*"%>
  <%@ page import="com.opensymphony.xwork2.ActionContext"%>
  <%@ page import="org.apache.struts2.dispatcher.multipart .MultiPartRequestWrapper"%>
 <%@ page import="org.json.simple.*"%>

  <%
    ///The file saving directory path img_upload is the directory name where the server stores uploaded images
   String savePath = request.getSession().getServletContext().getRealPath("/")+ "img_upload/ ";

     //File saving directory URL
  String saveUrl = request.getContextPath() + "/img_upload/";

     // Define the file extensions allowed for uploading
  String[] fileTypes = new String[] { "gif", "jpg", "jpeg", "png", "bmp" };

     //Maximum upload file size allowed
  long maxSize = 1024000;

     //Struts2 request wrapper filter
   MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) request;

     //Get the uploaded file name
  String fileName = wrapper.getFileNames("imgFile")[0];

     //Get file filter
  File file = wrapper.getFiles("imgFile")[0];

     //Get the extension of the uploaded file
  String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();

                  if (!Arrays. asList(fileTypes).contains(fileExt)) { name. "));
return;
Size exceeds limit "));
     return;
    }

     //Check the directory
   File uploadDir = new File(savePath);
  if (!uploadDir.isDirectory()) {
    out.println(getError("The upload directory does not exist.")) ;

    return; "));

return;
}

                                           ccoute] SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
   String newImgName = df.format(new Date()) + "_"+ new Random(). nextInt(1000) + "." + fileExt;

     //Set the image file address in KE
  String newFileName = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()
   + saveUrl + newImgName;

  byte[] buffer = new byte[1024];

     //Get the file output stream
  FileOutputStream fos = new FileOutputStream(savePath + newImgName);

     //Get the current file input stream in memory

  InputStream in = new FileInputStream(file);

        try {
         int num = 0;
           while ((num = in.read(buffer)) > 0) {
         fos.write(buffer, 0, num);
         }
        } catch (Exception e) {
         e.printStackTrace(System.err);
        } finally {
          in.close();
         fos.close();
        }

        //发送给 KE

        JSONObject obj = new JSONObject();
        obj.put("error", 0);
        obj.put("url", saveUrl + newImgName);
        out.println(obj.toJSONString());
        %>
        <%!private String getError(String message) {
         JSONObject obj = new JSONObject();
         obj.put("error", 1);
         obj.put("message", message);
         return obj.toJSONString();
        }
        %>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/364708.htmlTechArticle由于struts框架对request对象做了封装,原来处理上传图片的upload_json.jsp文件无法使用了,于是对kindeditor中处理上传图片的upload_json.jsp文件进...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn