struts2图片上传与下载

package com.zking.five.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.zking.four.web.BaseAction;

/**
 * 文件上传的三种方案:
 * 1、将上传的文件存放到数据库,以二进制的形式    oa系统   activity工作流框架
 * 2、将文件上传到文件服务器(硬盘U足够大)中
 * 3、将文件上传到tomcat所在的普通的web服务器
 * 
 * 
 * 真实路径与虚拟路径的概念
 * 1、所谓真实路径指的是在自己电脑上找得到的路径
 * 2、所谓虚拟路径指的是在自己电脑上找不到的路径,在别人电脑里面存在的路径
 * @author a
 *
 */
public class UploadAction extends BaseAction{
	private File file;//变量名指的是jsp的name属性,就是你要上传的属性
	private String fileContentType;
	private String fileFileName;
	
	private String serverDir = "/uplaod";
	/*public String execute() {
		System.out.println("hello Action");
		return null;
	}*/
	
	//文件上传
	public String upload() {
		/**
		 * srcFile  参数1:指的是本地文件
		 * destFile 参数2:指的是在服务器生成的文件
		 */
		String realPath = getRealPath(serverDir + "/" +fileFileName);
		System.out.println(fileContentType);
		System.out.println(fileFileName);
		System.out.println(realPath);
		try {
			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 = "2.jpg";
		response.setContentType(type);
		response.setHeader("Content-Disposition","filename=" + name);
		String realPath = getRealPath(serverDir + "/" +name);
		/**
		 * 将远程的图片输出到本地
		 * 数据源inputstream:远程 new file(realPath)
		 * 目的:输出到本地的jsp  response.getoutputstream 
		 */
		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;
	}
	
	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 String download() {
		String type = "image/jpeg" ;
		String name = "2.jpg";
		response.setContentType(type);
		response.setHeader("Content-Disposition","attachment;filename=" + name);
		String realPath = getRealPath(serverDir + "/" +name);
		/**
		 * 将远程的图片输出到本地
		 * 数据源inputstream:远程 new file(realPath)
		 * 目的:输出到本地的jsp  response.getoutputstream 
		 */
		try {
			FileUtils.copyFile(new File(realPath), response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	
	
	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;
	}
	
	
	
	

}

猜你喜欢

转载自blog.csdn.net/cg_9647/article/details/83180551