S 2 SHでstruts 2ファイルアップロードのaction


/**
 *            。     :        。             。
 *   struts      request,       MultiPartRequestWrapper        。
 *       kindeditor jsp  upload_json.jsp       。           ,   action     。
 * 
 * @author helei
 * @date 2014 6 20 13:54:13
 * @version v1.0
 * 
 */
public class UploadAction extends BaseStruts2Action {
	private static final long serialVersionUID = 1L;
	//   
	private File filedata;
	//       
	private String filedataContentType;
	//    
	private String filedataFileName;

	/**
	 *        action.    action               .        json     
	 *    url    type     . type=content     .type=image      
	 * 
	 * @return    ajax  .
	 * @throws FileUploadException
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public void uploadImg() throws Exception {
		//         
		String savePath = getRequest().getSession().getServletContext().getRealPath("/") + "upload/";
		//       URL
		String saveUrl = getRequest().getContextPath() + "/upload/";

		//             
		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,txt,zip,rar,gz,bz2");

		//       ,   struts2.xml     
		long maxSize = 3000000000l;
		//          
		getResponse().setContentType("text/html; charset=UTF-8");
		//      (       ),              
		String dirName = getRequest().getParameter("dir");
		if (dirName == null) {
			dirName = "image";
		}
		if (!extMap.containsKey(dirName)) {
			Common.printforajax(getError("      。"));
			return;
		}
		//      
		String tpath = FileUtil.mkdirs(savePath, dirName);
		savePath += tpath;
		saveUrl += tpath;
		
		if (!ServletFileUpload.isMultipartContent(getRequest())) {
			Common.printforajax(getError("     。"));
			return;
		}
		//     
		File uploadDir = new File(savePath);
		if (!uploadDir.isDirectory()) {
			Common.printforajax(getError("       。"));
			return;
		}
		//        
		if (!uploadDir.canWrite()) {
			Common.printforajax(getError("         。"));
			return;
		}
		
		//           
		String fileExt = filedataFileName.substring(filedataFileName.lastIndexOf(".") + 1).toLowerCase();
		if (!Arrays.<String> asList(extMap.get(dirName).split(",")).contains(fileExt)) {
			Common.printforajax(getError("               。
" + extMap.get(dirName)+ " 。")); } // if (filedata.length() > maxSize) { Common.printforajax(getError(" 。")); return; } // : + String newFileName = FileUtil.createNewFileName(filedataFileName); // InputStream in = new FileInputStream(filedata); OutputStream fos = new FileOutputStream(savePath + newFileName); byte[] buffer = new byte[1024 * 1024]; int length = 0; while (-1 != (length = in.read(buffer))) { fos.write(buffer, 0, length); } fos.close(); in.close(); saveUrl += newFileName; JSONObject obj = new JSONObject(); obj.put("err", 0); obj.put("msg", saveUrl); Common.printforajax(obj.toJSONString()); } public File getFiledata() { return filedata; } public void setFiledata(File filedata) { this.filedata = filedata; } public String getFiledataContentType() { return filedataContentType; } public void setFiledataContentType(String filedataContentType) { this.filedataContentType = filedataContentType; } public String getFiledataFileName() { return filedataFileName; } public void setFiledataFileName(String filedataFileName) { this.filedataFileName = filedataFileName; } /** * , * * @param message * * @return */ @SuppressWarnings("unchecked") private String getError(String message) { JSONObject obj = new JSONObject(); obj.put("err", "-1"); obj.put("msg", message); return obj.toJSONString(); }