struts2文件上传下载

本文介绍了一种文件上传至服务器的方法,包括图片上传、下载及直接在页面打开的功能实现。通过使用Java的FileUtils进行文件操作,实现了文件的上传、下载和显示。文章详细解释了如何获取文件的真实路径,并通过BufferedInputStream和BufferedOutputStream处理二进制文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.zking.five;

import java.awt.image.BufferedImage;
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 org.apache.coyote.http11.filters.BufferedInputFilter;

import com.zking.test.web.BaseAction;

/**
 * 文件上传的三种方式 
 * 1.上传的图片以二进制保存到数据库(activiti,jbpm工作框架) oa系统
 * 2.将文件上传到指定到服务器的硬盘(cpu快,硬盘较大) 
 * 3.将文件上传到tomcat所在服务器(对应的静态资源服务器--上线不重启)
 * 
 * @author Administrator
 *
 */
public class fileAction extends BaseAction {
	// 这是jsp传递过来的具体文件
	private File file;
	// 文件名
	private String fileFileName;
	// 文件类型
	private String fileContentType;
	// 虚拟路径
	private String serverDir = "/upload";

	/**
	 * 上传图片至服务器
	 * 
	 * @return
	 * @throws IOException
	 */
	public String upload() throws IOException {
		System.out.println("fileFileName:" + fileFileName);
		System.out.println("fileContentType:" + fileContentType);
		String realPath = getRealPath(serverDir + "/" + fileFileName);
		System.out.println("真实路径:" + realPath);
		FileUtils.copyFile(file, new File(realPath));
		return "success";
	}

	/**
	 * 获取文件夹的真实路径
	 * 
	 * @param path
	 * @return
	 */
	private String getRealPath(String path) {
		return this.request.getServletContext().getRealPath(path);
	}

	/**
	 * 下载图片
	 * 
	 * @return
	 * @throws IOException 
	 */
	public String saveAs() throws IOException {
		// 从数据库获取fileName,fileType
		String fileName = "s1.jpg";
		String fileType = "image/jpg";
		response.setContentType(fileType);
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
		String realPath = getRealPath(serverDir + "/" + fileName);
		// 从服务器获取图片写到本地
		FileUtils.copyFile(new File(realPath), response.getOutputStream());
		return null;
	}
	
	/**
	 * 直接在页面打开图片
	 * 
	 * @return
	 * @throws IOException
	 */
	public String openAs() throws IOException {
		// 从数据库获取fileName,fileType
		String fileName = "s2.jpg";
		String fileType = "image/jpg";
		response.setContentType(fileType);
		response.setHeader("Content-Disposition", "filename=" + fileName);
		String realPath = getRealPath(serverDir + "/" + fileName);
		// 从服务器获取图片写到本地
//		FileUtils.copyFile(new File(realPath), response.getOutputStream());
		BufferedInputStream in =new BufferedInputStream(new FileInputStream(new File(realPath)));
		BufferedOutputStream out=new BufferedOutputStream(response.getOutputStream());
		copyBufStream(in, out);
		return null;
	}
	/**
	 * copy二进制文件用的
	 * @param in	从那里来
	 * @param out	写到那里去
	 * @throws IOException 
	 */
	private void copyBufStream(BufferedInputStream in,BufferedOutputStream out) throws IOException {
		byte[] buf=new byte[1024];
		int len=0;
		while((len=in.read(buf))!=-1) {
			out.write(buf,0,len);
		}
		in.close();
		out.close();
	}
	
	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;
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值