Home > Article > Backend Development > The problem of unable to upload images when integrating kindeditor and Struts2 framework_PHP tutorial
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.
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;
}
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);
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();
}
%>