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;
}
}
struts2文件上传下载
最新推荐文章于 2024-06-29 22:48:48 发布