富文本框TinyMCE4.8上传本地图片基本配置(后端篇)

哎,这个后端篇拖了一个月终于要开始了,现在想想发现自己的拖延症其实好严重,也有其他原因导致现在才想开始写博客。

废话少说,下面开始正题

说一下我用的是SSH框架,大家如果是使用PHP等开发语言其实逻辑是一样的。

先说一下逻辑思路:

xhr.open('POST', 'FileAction_updateFile');//第二个参数:如果是SSH框架就创建一个‘FileAction’的Action的‘updateFile’方法,如果是PHP页面就直接创建一个PHP页面,例如:updateFile.php,并将参数‘FileAction_updateFile’改为‘updateFile.php’

第一步:根据前台创建后台处理页面(FileAction.java)

第二步:上传文件/图片(如果是PHP页面,想必不用我教如何接收前台上传的文件/图片吧)

如果是SSH框架的话,框架会帮我们自动封装数据,我采用的是模型驱动,FileAction.java代码如下

package web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import domain.MyFile;

public class FileAction extends ActionSupport implements ModelDriven<MyFile>{
	MyFile file = new MyFile();//接收上传文件/图片的对象
	public String updateFile() throws Exception {
//下面代码是将接收到的文件/图片保存在其他地方(这里是该目录下的‘article\images’路径下)	
		String path=ServletActionContext.getServletContext().getRealPath("/")+"article\\images\\"+file.getFileFileName();		
		File oneFile=new File(path);
		if(!oneFile.getParentFile().exists()) {
			oneFile.getParentFile().mkdirs();
		}		
		oneFile.createNewFile();
		try {
			BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(oneFile));
			BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file.getFile()));
			int c;
			while((c=bis.read())!=-1) {
				bos.write(c);
				bos.flush();//刷新缓冲区
			}
			bis.close();
			bos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		ActionContext.getContext().getSession().put("path", file.getFileFileName());//将路径存放在session中;
		return SUCCESS;
	}
	@Override
	public MyFile getModel() {
		return file;
	}

}

简单说一下上面代码:MyFile file = new MyFile();是SSH框架自动将上传的文件/图片接收,生成一个MyFile对象,然后使用Java的I/O操作将文件复制到其他路径进行保存。

MyFile.java的代码如下:

package domain;

import java.io.File;

public class MyFile {
	private File file;
	private String fileFileName;
	private String fileContentType;
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
}

注意:SSH框架封装上传TinyMCE数据的必须包含file,fileFileName,fileContentType这三个属性(因为前端我们不能设置文件名字,就只能使用file,如果其他名字将无法封装数据)

第三步:文件/图片上传成功后跳转至JsonAction.java返回一组json数据给前台接收

扫描二维码关注公众号,回复: 3630493 查看本文章

先看一下struts.xml的主要配置:

<action name="FileAction_*" class="web.FileAction" method="{1}">
		<result name="success" type="redirect">JsonAction_returnJson</result>
</action>

说明一下:如果FileAction.java执行成功就重定向到JsonAction.java

JsonAction.java的代码如下:

package web;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.JsonInfo;

public class JsonAction extends ActionSupport implements ModelDriven<JsonInfo>{
	JsonInfo info=new JsonInfo();
	public String returnJson() throws Exception {
		String LJ ="article\\images\\"+ (String) ActionContext.getContext().getSession().get("path");
		if(LJ!=null) {			
			info.setLocation(LJ);			
		}
		return SUCCESS;
	}
	@Override
	public JsonInfo getModel() {
		return info;
	}
}

上面代码的意思主要是将复制后文件路径以json数据格式返回给前台(注意路径)

JsonInfo.java的代码如下:

package domain;

public class JsonInfo {
	private String location;
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}	
}

注意一定要location属性,还成其他的前台就无法识别。

总结一下:1.创建页面->2.接收前台文件/图片->3.复制文件->4.以json格式返回复制文件的路径


哎呀,今天好累,感觉思路有点混乱,大家如果不明白就留言吧,基本在一两天之内我就会为你解答的。

猜你喜欢

转载自blog.csdn.net/weixin_41660508/article/details/83185049
今日推荐