当前位置 : 主页 > 编程语言 > java >

文件的上传下载

来源:互联网 收集:自由互联 发布时间:2021-06-28
Struts框架下载,Action必须满足: Action中有一个InputStream类型的属性。提供get方法。 package com.tx.action;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOExce
Struts框架下载,Action必须满足: Action中有一个InputStream类型的属性。提供get方法。
package com.tx.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.tx.bean.FileInfo;
import com.tx.service.FileService;



public class FileAction {
	private File poto;
	private String potoFileName;
	private String potoContentType;
	private String fileUploadPath;
	private int fileId;
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
	private InputStream inputStream;
	private FileService fileservice=new FileService();
	/**
	 * 方法
	 * @return
	 * @throws IOException 
	 * @throws SQLException 
	 */
	public String name() throws IOException, SQLException{
		//设置时间
		String dirs = sdf.format(new Date());
		//保存时间+地址
		File file = new File(fileUploadPath+dirs);
		//如果他不存在
		if(!file.exists()){
			//重新建立一个
			file.mkdirs();
		}
		File name = new File(file+potoFileName);
		//同一个文件夹复制文件
		
		System.out.println(poto);
		
		FileUtils.copyFile(poto, name);
		//将数据存入数据库
		FileInfo fileInfo = new FileInfo(); 
		fileInfo.setFileName(potoFileName);
		fileInfo.setPath(dirs+potoFileName);
		fileInfo.setSize(poto.length());
		fileInfo.setType(potoContentType);		
		fileservice.saveFileInfo(fileInfo);
		
		return "tolist";
	}
	
	/**
	 * struts 框架下载
	 * @return
	 * @throws SQLException 
	 * @throws FileNotFoundException 
	 */
	public String download() throws Exception{
		FileInfo fileInfo=fileservice.getFileInfo(fileId);
		File file=new File(fileUploadPath+fileInfo.getPath());
		
		if(file.exists()){
			inputStream=new FileInputStream(file);
			potoFileName = new String(fileInfo.getFileName().getBytes("utf-8"),"ISO-8859-1");
			return "down";
		}else{
			ServletActionContext.getRequest().setAttribute("error", "文件找不到");
			return "error";
		}
	}
	//预览

	public String loadveiw() throws Exception{
		FileInfo fileInfo=fileservice.getFileInfo(fileId);
		File file=new File(fileUploadPath+fileInfo.getPath());
		
		if(file.exists()){
			inputStream=new FileInputStream(file);
			potoFileName = new String(fileInfo.getFileName().getBytes("utf-8"),"ISO-8859-1");
			return "veiw";
		}else{
			ServletActionContext.getRequest().setAttribute("error", "文件找不到");
			return "error";
		}
		
	}
	//遍历 保存 返回list   
	public String list() throws SQLException{
		List
 
   fileList = fileservice.findAll();
		ServletActionContext.getRequest().setAttribute("fileList", fileList);
		return "list";
	}

	
	/**
	 * get / set
	 * @param file
	 */


	public File getPoto() {
		return poto;
	}


	public String getPotoFileName() {
		return potoFileName;
	}


	public void setPotoFileName(String potoFileName) {
		this.potoFileName = potoFileName;
	}


	public void setPoto(File poto) {
		this.poto = poto;
	}




	public String getPotoContentType() {
		return potoContentType;
	}


	public void setPotoContentType(String potoContentType) {
		this.potoContentType = potoContentType;
	}


	public String getFileUploadPath() {
		return fileUploadPath;
	}

	public void setFileUploadPath(String fileUploadPath) {
		this.fileUploadPath = fileUploadPath;
	}


	public InputStream getInputStream() {
		return inputStream;
	}


	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public int getFileId() {
		return fileId;
	}

	public void setFileId(int fileId) {
		this.fileId = fileId;
	}
	
}
 
上一篇:MD5加密
下一篇:自动补全
网友评论