struts2图片上传与下载

本文介绍了一种文件上传处理的方法,包括将文件存放在数据库、文件服务器或web服务器上的三种策略。详细展示了如何使用Java和Apache Commons IO库进行文件上传、下载及在服务器间传输的操作。

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

package com.zk.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;
	}
	
	
	
	

}

/* * 使用java.util.Map接口实现文件组的上传 */ private void muchUploadFile(IndexActionForm objForm) { Map<String, FormFile> fileList = objForm.getFileList(); for(String str : fileList.keySet()) if((fileList.get(str)).getFileSize() > 0 && (fileList.get(str)).getFileSize() < BUFFER_SIZE) { String fileName = DIRECTORY +"/"+ fileList.get(str).getFileName(); try { this.fileStream( fileList.get(str).getInputStream(), fileName, fileList.get(str).getFileSize()); } catch (IOException ex) { System.out.println(ex.getMessage()); } } } /* * 使用org.apache.strtus.upload.FormFile实现文件的单一上传 */ private void singleUploadFile(IndexActionForm objForm) { if(objForm.getFile().getFileSize() == 0|| BUFFER_SIZE < objForm.getFile().getFileSize()) throw new RuntimeException("文件过大或不存在!!!"); String fileName = DIRECTORY +"/"+ objForm.getFile().getFileName(); try { this.fileStream( objForm.getFile().getInputStream(), fileName, objForm.getFile().getFileSize()); } catch (IOException ex) { System.out.println(ex.getMessage()); } } /* * copy 到本地目录 */ private void fileStream(InputStream strem, String fileName, int size) { byte[] buffer = new byte[size]; try { InputStream in = null; OutputStream out = null; try{ in = new BufferedInputStream(strem, size); out = new BufferedOutputStream(new FileOutputStream(fileName), size); while(in.read(buffer) > 0) out.write(buffer); }finally { if(null != in) in.close(); if(null != out){ out.flush(); out.close(); } } System.out.println("Uploading Success!!!"); } catch (IOException ex) { System.out.println(ex.getMessage()); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值