如果想知道struts2文件下载可以登录以下地址:https://blog.csdn.net/qq_42694534/article/details/84204831
最近有人问struts2文件怎么上传,现在就跟大家分享一下啦!
文件上传的三种方式 :
1.上传的图片以二进制保存到数据库
2.将文件上传到指定到服务器的硬盘(cpu快,硬盘较大)
3.将文件上传到tomcat所在服务器(对应的静态资源服务器–上线不重启)
这是前端代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
//文件上传
<form action="${pageContext.request.contextPath}/sy/fileAction_upload.action" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
//打开已经上传的文件
<img src="${pageContext.request.contextPath }/sy/fileAction_openAs.action"/>
</body>
</html>
后端的action代码如下:
注意:这里需要到commons-io-2.5.jar,commons-fileupload-1.3.3.jar(文件上传与下载所使用的包)
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;
public class fileAction extends BaseAction {
// 这是jsp传递过来的具体文件
private File file;
// 文件名
private String fileFileName;
// 文件类型
private String fileContentType;
// 虚拟路径,upload是要保存的文件夹
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 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;
}
}