struts上传图片和显示图片以及下载图片

  • 文件上传的三种方案:
  • 1、将上传的文件以二进制的形式存放到数据库
  • 2、将文件上传到文件服务器(硬盘足够大)中
  • 3、将文件上传到tomcat所在的普通web服务器
  • 真实路径与虚拟路径的概念
  • 1、所谓真实路径指的是在自己电脑上能够找到的路径
  • 2、所谓虚拟,在自己电脑上是看不到的,路径在别人的电脑(tomcat所在位置)上能够看到

demo1:

<form action="${pageContext.request.contextPath }/sy/uploadAction_upload.action" enctype="multipart/form-data" method="post">
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>

uploadAction 下载跟显示其实都一样,就多了一个(attachment;)单词:

	public class UploadAction extends BaseAction{
	private File file;  //变量名指的是jsp的name属性,就是你要上传的文件  xxx
	private String fileContentType; //xxxfileContentType 后面是固定的
	private String fileFileName; // xxxfileFileName
	
	private String serverDir="/upload";


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


	
	//上传
	public String upload(){
	System.out.println(fileContentType);  //图片路径
	System.out.println(fileFileName);	//图片名字
	String realPath=getRealPath(serverDir + "/" +fileFileName);
	System.out.println(realPath);
	try {
		/*
		 * 参数1:本地图片文件
		 * 参数2:在服务器生成的文件
		 */
		FileUtils.copyFile(file, new File(realPath));
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return SUCCESS;
}

	/**
	 * 获取Linux下的上传文件的所在位置
	 * @param path
	 * @return
	 */
	private String getRealPath(String path) {
		// TODO Auto-generated method stub
		return application.getRealPath(path);
	}

//显示
public String openAs(){
	String type="image/jpeg";
	String name="1.jpg";
	response.setContentType(type);
	 response.setHeader("Content-Disposition","filename=" + name);//文件名
	/*
	 * 将远程的图片输出到本地
	 * 数据源inputstream:远程 new file(realPath)
	 * 目的:输出到本地的jsp response.getOupStream
	 */
	String realPath=getRealPath(serverDir+ "/" +name);
	try {
//			FileUtils.copyFile(new File(realPath), response.getOutputStream());    //注意,Buff缓冲流速度快。如果不需要可注释掉下面代码
			BufferedInputStream in=new BufferedInputStream(new FileInputStream(	new File(realPath)));
			BufferedOutputStream out=new BufferedOutputStream(response.getOutputStream());
			copyStream(in, out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	private void copyStream(BufferedInputStream in,BufferedOutputStream out) throws IOException{
	byte[] bbuf=new byte[1024];
	int len=0;
	while ((len=in.read(bbuf))!=-1) {
		out.write(bbuf, 0 , len);

	}
	in.close();
	out.close();
}

	//下载
	public String download(){
	String type="image/jpeg";
	String name="1.jpg";
	response.setContentType(type);
	// 如果要下载 必须要加 attachment;  直接打开不需要
	 response.setHeader("Content-Disposition","attachment;filename=" + name);//文件名
	/*
	 * 将远程的图片输出到本地
	 * 数据源inputstream:远程 new file(realPath)
	 * 目的:输出到本地的jsp response.getOupStream
	 */
	String realPath=getRealPath(serverDir+ "/" +name);
	try {
		FileUtils.copyFile(new File(realPath), response.getOutputStream());
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
	}

}

配置:

<action name="uploadAction_*" class="com.five.interceptor.UploadAction" method="{1}">
		  	<result name="success">/success.jsp</result>
		  </action> 

BaseAction:

public class BaseAction implements ServletRequestAware, ServletResponseAware{
	/**
	 * 为了传值使用
	 */
	protected HttpServletResponse response;
	protected HttpServletRequest request;
	protected HttpSession session;
	protected ServletContext application;
	
	/**
	 * 为了配置跳转页面所用
	 */
	 	protected final static String UPLOAD = "upload";
}

success.jsp:

<h2>直接打开图片</h2>
<s:url var="openAsUrl" namespace="/sy" action="uploadAction_openAs.action"/>
<s:property value="openAsUrl"/>
<img alt="" src="<s:property value="openAsUrl"/>">

<h2>下载图片</h2>
<s:url var="downloadUrl" namespace="/sy" action="uploadAction_download.action"/>
<s:a href="%{#downloadUrl}">下载</s:a>

demo1.jsp界面:
在这里插入图片描述

success.jsp界面:
在这里插入图片描述

注意:
这都是定死的,变成数据库灵活使用的都差不多,就是用实体类的set获取其值,再根据传来的id获取值。
列如:String type=request.getParameter(“image”);

  1. 内容类型
    response.setContentType(d.getMime());

  2. 设置响应头
    response.setHeader(“Content-Disposition”,“attachment;filename=” + fileName);//文件名

  3. 处理文件名的中文乱码
    String fileName = d.getFileName();
    fileName = new String(fileName.getBytes(“utf-8”), “iso8859-1”);

  4. struts2文件上传大小设置

  5. struts2文件上传类型设置
    根据struts2自带的fileupload拦截器中提供的allowedTypes来进行限制

    image/png,image/gif,image/jpeg

切记:有时候可能会因为缓存原因导致下载或者显示不出来,记得关闭控制台,清理一下缓存然后重新执行。

猜你喜欢

转载自blog.csdn.net/qq_43163499/article/details/83099294