一、Struts2文件上传
文件上传注意:
- 表单method必须是post。
- 使用二进制编码.multipart/form-data。
Student实体类
package com.zking.s03.entity;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private Integer studentId;
private String studentName;
private String sex;
private String remark;
private Integer cid;
private String clazzName;
private String photo;
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getClazzName() {
return clazzName;
}
public void setClazzName(String clazzName) {
this.clazzName = clazzName;
}
public Student() {}
public Student(Integer studentId, String studentName, String sex, String remark, Integer cid, String clazzName) {
this.studentId = studentId;
this.studentName = studentName;
this.sex = sex;
this.remark = remark;
this.cid = cid;
this.clazzName = clazzName;
}
@Override
public String toString() {
return "Student [studentId=" + studentId + ", studentName=" + studentName + ", sex=" + sex + ", remark="
+ remark + ", cid=" + cid + ", clazzName=" + clazzName + ", photo=" + photo + "]";
}
}
FileAction.java
package com.zking.s03.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import com.zking.s03.dao.FileDAO;
import com.zking.s03.dao.StudentDAO;
import com.zking.s03.entity.MyFile;
import com.zking.s03.entity.Student;
public class FileAction extends BaseAction {
private Integer studentId;
private String fileId;
public Integer getStudentId() {
return studentId;
}
public String getFileId() {
return fileId;
}
public void setFileId(String fileId) {
this.fileId = fileId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
private String savePath = "/uploads";
private StudentDAO studentDAO = new StudentDAO();
private FileDAO fileDAO = new FileDAO();
private File photo;
private String photoFileName;
private String photoContentType;
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public String upload() throws Exception {
// 1.将上传文件信息保存到数据库
MyFile myFile = new MyFile();
myFile.setRealName(this.photoFileName);
myFile.setContentType(this.photoContentType);
String fileId = fileDAO.add(myFile);
// 2.将上传文件保存到目标服务器
String path = savePath + "/" + this.photoFileName;
String realPath = this.getRealPath(path);
InputStream is = new FileInputStream(photo);
OutputStream os = new FileOutputStream(new File(realPath));
byte[] b = new byte[1024];
System.out.println("b="+b);
int len = -1;
while (-1 != (len = is.read(b))) {
os.write(b, 0, len);
}
is.close();
os.close();
// 3.将上传文件绑定到对应的对象中去
Student student = new Student();
System.out.println("student:"+student);
student.setStudentId(this.studentId);
student.setPhoto(fileId);
studentDAO.updatePhoto(student);
return SUCCESS;
}
getRealPath(String path) {
return application.getRealPath(path);
}
}
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="sys" namespace="/sys" extends="base">
<action name="fileAction_*" class="com.zking.s03.action.FileAction" method="{1}">
<result name="success" type="redirect">
/sys/studentAction_list.action
</result>
</action>
</package>
</struts>
代码演示:
<form action="sys/fileAction_upload.action" method="post" enctype="multipart/form-data">
<input type="file" name="photo"> <br>
<input type="hidden" name="studentId" value="${studentId}">
<input type="submit" value="上传">
</form>
文件下载:FileAction.java
获取文件写入流saveAs()
package com.zking.s03.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import com.zking.s03.dao.FileDAO;
import com.zking.s03.dao.StudentDAO;
import com.zking.s03.entity.MyFile;
import com.zking.s03.entity.Student;
public class FileAction extends BaseAction {
private Integer studentId;
private String fileId;
public Integer getStudentId() {
return studentId;
}
public String getFileId() {
return fileId;
}
public void setFileId(String fileId) {
this.fileId = fileId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
private String savePath = "/uploads";
private StudentDAO studentDAO = new StudentDAO();
private FileDAO fileDAO = new FileDAO();
private File photo;
private String photoFileName;
private String photoContentType;
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public String upload() throws Exception {
// 1.将上传文件信息保存到数据库
MyFile myFile = new MyFile();
myFile.setRealName(this.photoFileName);
myFile.setContentType(this.photoContentType);
String fileId = fileDAO.add(myFile);
// 2.将上传文件保存到目标服务器
String path = savePath + "/" + this.photoFileName;
String realPath = this.getRealPath(path);
InputStream is = new FileInputStream(photo);
OutputStream os = new FileOutputStream(new File(realPath));
byte[] b = new byte[1024];
System.out.println("b="+b);
int len = -1;
while (-1 != (len = is.read(b))) {
os.write(b, 0, len);
}
is.close();
os.close();
// 3.将上传文件绑定到对应的对象中去
Student student = new Student();
System.out.println("student:"+student);
student.setStudentId(this.studentId);
student.setPhoto(fileId);
studentDAO.updatePhoto(student);
return SUCCESS;
}
/**
* 另存为(下载)
*
* @return
* @throws Exception
*/
public String saveAs() throws Exception {
// 1.查询下载文件的详细信息
MyFile myFile = new MyFile();
myFile.setFileId(this.fileId);
MyFile f = fileDAO.load(myFile);
String realName = f.getRealName();
realName = new String(realName.getBytes("utf-8"), "iso8859-1");
String contentType = f.getContentType();
// 2.文件下载
String path = savePath + "/" + realName;
String realPath = this.getRealPath(path);
System.out.println(realPath);
// 响应头设置
// 1. 内容类型
response.setContentType(contentType);
// 2. 设置响应头
response.setHeader("Content-Disposition", "attachment;filename=" + realName);// 文件名
InputStream is = new FileInputStream(new File(realPath));
OutputStream os = response.getOutputStream();
byte[] b = new byte[1024];
int len = -1;
while (-1 != (len = is.read(b))) {
os.write(b, 0, len);
}
is.close();
os.close();
return null;
}
// 将虚拟路径转化为真实路径
public String getRealPath(String path) {
return application.getRealPath(path);
}
}
代码演示:
<a href="sys/fileAction_saveAs.action?fileId=${s.photo}">另存为</a>