struts2 文件上传与下载

文件上传:
三种上传方案
虚拟路径与真实路径 /upload
copyFile与copydirectory
文件上传的三种方案

  • 1.将上传的文件以二进制的形式存放到数据库 oa系统 activiti工作流框架
  • 2.将文件上传到文件服务器(硬盘足够大)中
  • 3.上传到tomcat所在的普通Web服务器中
  • 真实路径与虚拟路径的概念
  • 1.所谓真实路径指的是自己电脑上能够找到的路径
  • 2.所谓虚拟,在自己电脑上看不到的,路径在别人的电脑(tomcat所在地址)上能够看到的
  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文件上传大小设置

    指定允许上传的文件最大字节数。默认值是2097152(2M) 10M=1010241024

    <constant name="struts.multipart.maxSize" value="10485760"/>

  5. struts2文件上传类型设置

    根据struts2自带的fileupload拦截器中提供的allowedTypes来进行限制

    <interceptor-ref name="fileUpload">
         <param name="allowedTypes">image/png,image/gif,image/jpeg
         </param></interceptor-ref>
    
  6. 其它

    enctype=“multipart/form-data” method=“post”
    private File file;
    private String fileContentType;
    private String fileFileName;

package src.com.zking.five.interceptor;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import org.apache.commons.io.FileUtils;

import src.com.zking.fore.web.BaseAction;
import src.com.zking.fore.web.dao.StudentDAO;
import src.com.zking.fore.web.entity.Student;


/**
 * 文件上传的三种方案
 * 	1.将上传的文件以二进制的形式存放到数据库     oa系统   activiti工作流框架
 * 	2.将文件上传到文件服务器(硬盘足够大)中
 *  3.上传到tomcat所在的普通Web服务器中
 * @author YGUUU
 *
 *真实路径与虚拟路径的概念
 *   1.所谓真实路径指的是自己电脑上能够找到的路径
 *   2.所谓虚拟,在自己电脑上看不到的,路径在别人的电脑(tomcat所在地址)上能够看到的
 */
public class UploadAction extends BaseAction{
	private StudentDAO sd=new StudentDAO();
	private Student student=new Student();
	
	public File file;//变量名指的是jsp的name属性,就是你要上传的文件   xxx
	public String fileContentType;//xxxContentType
	public String fileFileName;//xxxFileName
	
	private String serverDir="/upload";
	
	
//	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) {
		return application.getRealPath(path);
	}

//	public String open() {
//		String type="image/jpeg";
//		String name="22-16042915064O11.jpg";
//		response.setContentType(type);
//		response.setHeader("Content-Disposition","attachment;filename=" + name);//文件名
//		/**
//		 * 将远程的图片输出到本地
//		 *    数据源inputstream:远程  new File(realPath)
//		 *    目的:输出到本地的jsp    response.getOutputStream
//		 */
//		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;
//	}
	
	
//	public String download() {
//		String type="image/jpeg";
//		String name="22-16042915064O11.jpg";
//		response.setContentType(type);
//		response.setHeader("Content-Disposition","filename=" + name);//文件名
//		/**
//		 * 将远程的图片输出到本地
//		 *    数据源inputstream:远程  new File(realPath)
//		 *    目的:输出到本地的jsp    response.getOutputStream
//		 */
//		String realPath= getRealPath(serverDir+"/"+name);
//		try {
//			//FileUtils.copyFile(new File(realPath), response.getOutputStream());
//			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;
//	}

	/**
	 * 加快文件读取速度
	 * @param in
	 * @param out
	 */
	private void copyStream(BufferedInputStream in,BufferedOutputStream out) {
		byte[] bbuf=new byte[1024];
		int len=0;
		try {
			while((len=in.read(bbuf))!=-1) {
				out.write(bbuf,0,len);
			}
			in.close();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	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;
	}

}

图片直接显示,上传与下载jsp代码

<%@page import="com.opensymphony.xwork2.ActionContext"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
 跳转成功

 <h2>文件选择器</h2>
 <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>
 <h2>直接打开图片</h2>
 <s:url var="openAsUrl" namespace="/sy" action="uploadAction_open.action"></s:url>
 <s:property value="openAsUrl"/>
 <img alt="" src='<s:property value="openAsUrl"/>'>
 
 <h2>另存为(下载)</h2>
 <s:url var="downloadAsUrl" namespace="/sy" action="uploadAction_download.action"></s:url>
 <s:a href="%{#downloadAsUrl}">下载</s:a>
 
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值