struts文件上传

利用struts完成文件上传,两三行代码即可完成操作。

public class BaseAction implements ServletRequestAware,ServletResponseAware{
 
	//一些必须的属性 拿到request的一些属性
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	protected ServletContext application;
	
	//每一个action里方法处理完返回的结果码
	protected final String SUCCESS="success";//处理成功
	protected final String FAIL="fail";//或失败
	protected final String LIST="list";//查询所有的结果码
	protected final String PREADD="preAdd";//去增加界面结果码
	protected final String PREUPDATE="preupdate";//去修改界面结果码
	protected final String TOLIST="tolist";//增删改返回进去的结果码
	//返回的结果集
	protected Object result;
	protected Object msg;
	protected int code;
	
	
	
	public Object getResult() {
		return result;
	}
 
	public void setResult(Object result) {
		this.result = result;
	}
 
	public Object getMsg() {
		return msg;
	}
 
	public void setMsg(Object msg) {
		this.msg = msg;
	}
 
	public int getCode() {
		return code;
	}
 
	public void setCode(int code) {
		this.code = code;
	}
 
	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response=response;
	}
 
	@Override//在方法里给他们赋值
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request=request;
		this.session=request.getSession();
		this.application=request.getServletContext();
	}
 
}

文件上传三种方式

1:上传图片以二进制形式保存到数据库

2.将图片上传到指定服务器的硬盘(cpu快 硬盘教大)

3.将图片上传到tomcat所在服务器(对应静态资源服务器——上线不重启)

下面这种就是用的第三种来事例 通过虚拟路径拿到真实路径

然后写文件上传的代码逻辑:一般来说文件上传应该是从数据库拿值 我这就在本地设置了图片的名字了

public class FileAction extends BaseAction{
	private File file;//jsp页面传过来的文件   三个必须的属性
	private String fileFileName;//文件名
	private String fileContentType;//文件类型
 
	private String dir="/upload";//虚拟路径
	
	
	//封装
	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;
	}
 
	//文件上传
	public String upload() {
		System.out.println("filefilename:"+fileFileName);//输出那个文件名字
		System.out.println("filetype:"+fileContentType);//输出拿到的类型
//这里说实话就两句代码实现了上传的功能
		String realpath=getrealpath(dir+"/"+fileFileName);//用下面的方法拿到虚拟路径加这个文件名
		System.out.println("realpath:"+realpath);
		try {
			FileUtils.copyFile(file, new File(realpath));//这个文件上传到新的文件路径
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";
		
	}
	
	/**
	 * 获取文件夹真实路径
	 * @param path
	 * @return
	 */
	public String getrealpath(String path) {//获取全项路径
		return this.request.getServletContext().getRealPath(path);
	}
	
	//下载图片
	public String saveAs() {
		//这里应该从数据库拿值
				String fileName="stationmaster_img.png";
				String filetypes="img/png";
				response.setContentType(filetypes);//设置图片类型
				//打开图片不要加这个attachment;下载需要
				response.setHeader("Content-Disposition","attachment;filename=" + fileName);//添加头文件
				String realpath=getrealpath(dir+"/"+fileName);//用下面的方法拿到虚拟路径加这个文件名
				//从服务器获取到的图片下载到本地
				try {
					FileUtils.copyFile(new File(realpath), response.getOutputStream());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return null;
	}
	
	
	//打开图片
	public String openAs() {
		//这里应该从数据库拿值
		String fileName="stationmaster_img.png";
		String filetypes="img/png";
		response.setContentType(filetypes);//设置图片类型
		//打开图片不要加这个attachment;下载需要
		response.setHeader("Content-Disposition","filename=" + fileName);//添加头文件
		String realpath=getrealpath(dir+"/"+fileName);//用下面的方法拿到虚拟路径加这个文件名
		//从服务器获取到的图片写到本地
		try {//服务器到本地输出这个文件
			FileUtils.copyFile(new File(realpath), response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
 
}

页面上需要写一个上传文件的文件选择器

<h2>文件上传</h2>//调用这个路径方法下载图片然后可以跳到那个界面
<form action="${pageContext.request.contextPath}/sy/fileAction_upload.action" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<input type="submit">
</form>
 
 
 
 
//这里是struts配置里已经配置好了图片下载过去的路径
	<action name="fileAction_*" class="com.struts.action.FileAction" method="{1}">
	<!-- 如果这个action要用到就调用这个拦截器 -->
	<!-- <interceptor-ref name="oneinterceptor"></interceptor-ref> -->
	<result name="success">/a.jsp</result>
	</action>

图片展示的地方

<body>
测试:${rs}
查看图片 这就是查看跟下载然后调用的方法
<img alt="" src="${pageContext.request.contextPath}/sy/fileAction_openAs.action">
<a href="${pageContext.request.contextPath}/sy/fileAction_saveAs.action">下载图片</a>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值