使用struts2实现文件上传与下载功能

时间:2021-11-01 08:03:43

这个问题做了两天,在网上找了很多例子,但是还有一些功能没有实现,暂时先把代码贴出来,以后在做这方面的功能时在修改

文件上传:

一开始我在网上找到基于servlet+jsp环境写的文件上传,但是在将页面表单获得的文件数据放入List<FileItem>list集合中,一直是空的,后来才知道是因为struts2对request对象进行封装,由httpServletRequest变成

MultiPartRequestWrapper。导致fileList=upload.parseRequest(request);获取不到上传的对象,但是我根据网上的解决方法依旧没有解决,最后换了其他方法,直接用struts2对上传文件的方法

直接贴文件上传代码:

Action代码:

package com.java.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.java.web.model.User;
import com.java.web.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UploadFileAction extends ActionSupport{
private static final long serialVersionUID = 1L;

private String uploadName;
private File filePhoto;
private String filePhotoFileName;
private String filePhotoFileType;
//private Map<String,Object> messageAjax;
HttpServletRequest request=ServletActionContext.getRequest();

private UserService userService;

public String uploadFile() throws Exception{
String uid=request.getParameter("uid");

//request.setAttribute("uid", uid);
User u=null;

u= userService.getUser(uid);
request.setAttribute("user", u);

return SUCCESS;
}

//@SuppressWarnings("deprecation")
public String uploadFileSave() throws Exception{
String uid=request.getParameter("uid");
//messageAjax=new HashMap<String,Object>();

if(filePhoto==null){
request.setAttribute("message", "文件上传失败!请选择文件");
return SUCCESS;
}

//获取文件存储路径
//String path=ServletActionContext.getServletContext().getRealPath("/upload");
String path1="F:"+"\\workspaces"+"\\SSH_WEB_1"+"\\WebRoot"+"\\upload";
//request.setAttribute("fileName", filePhotoFileName);

String fileName=filePhotoFileName.substring(filePhotoFileName.lastIndexOf("\\")+1);
//得到扩展名
String fileExtName=fileName.substring(fileName.lastIndexOf(".")+1);
System.out.println(fileExtName);
if(fileExtName.equalsIgnoreCase("png")||fileExtName.equalsIgnoreCase("jpg")){
/*for(int i=0;i<file1.size();i++){*/
OutputStream os=new FileOutputStream(new File(path1,filePhotoFileName));
InputStream is=new FileInputStream(filePhoto);

userService.savePic(is,uid);

User u = userService.getUser(uid);
Blob picture = u.getPicture();

//request.setAttribute("", arg1);
long size=picture.length();
byte[] b=picture.getBytes(1, (int) size);

byte[] buffer=new byte[1024];
//判断输入流中的数据是否已经读完的标识
int len=0;
//循环将输入流读入缓冲区,(len=is.read(buffer)>0)标识is中还有数据
while((len=is.read(buffer))>0){
os.write(buffer,0,len);
}
is.close();
os.close();
//}

}else{
/*messageAjax.put("messageFile","上传文件类型错误,请选择png,jpg类型文件");*/
request.setAttribute("message", "上传文件类型错误,请选择png,jpg类型文件");
return SUCCESS;
}

String imgPath=filePhotoFileName;

request.setAttribute("imgPath", imgPath);

//messageAjax.put("messageFile","上传成功!");
request.setAttribute("message", "上传成功!");
return SUCCESS;

}

/**
* @return the uploadName
*/
public String getUploadName() {
return uploadName;
}

/**
* @param uploadName the uploadName to set
*/
public void setUploadName(String uploadName) {
this.uploadName = uploadName;
}

/**
* @return the filePhoto
*/
public File getFilePhoto() {
return filePhoto;
}

/**
* @param filePhoto the filePhoto to set
*/
public void setFilePhoto(File filePhoto) {
this.filePhoto = filePhoto;
}

/**
* @return the filePhotoFileName
*/
public String getFilePhotoFileName() {
return filePhotoFileName;
}

/**
* @param filePhotoFileName the filePhotoFileName to set
*/
public void setFilePhotoFileName(String filePhotoFileName) {
this.filePhotoFileName = filePhotoFileName;
}

/**
* @return the filePhotoFileType
*/
public String getFilePhotoFileType() {
return filePhotoFileType;
}

/**
* @param filePhotoFileType the filePhotoFileType to set
*/
public void setFilePhotoFileType(String filePhotoFileType) {
this.filePhotoFileType = filePhotoFileType;
}

/**
* @return the userService
*/
public UserService getUserService() {
return userService;
}

/**
* @param userService the userService to set
*/
public void setUserService(UserService userService) {
this.userService = userService;
}

}

struts.xml配置:

使用struts2实现文件上传与下载功能

jsp页面代码:

使用struts2实现文件上传与下载功能使用struts2实现文件上传与下载功能

使用struts2实现文件上传与下载功能

文件下载代码:

Action代码:

package com.java.web.action;

import java.io.File;
import java.io.InputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadFileAction extends ActionSupport{
private static final long serialVersionUID = 1L;

HttpServletRequest request=ServletActionContext.getRequest();

//一:显示所有要下载的文件列表
public String downLoadFileList() throws Exception{
//1.得到目录路径
String path=ServletActionContext.getServletContext().getRealPath("/WEB-INF/download");
//2.目录对象
File file=new File(path);
//3.得到所有下载文件的文件名
String[] fileName=file.list();
//4.保存
request.setAttribute("fileName", fileName);
return "list";
}

//二:文件下载
//1.获取下载文件的文件名,设置字符集
private String fileName;
public void downloadFile(String fileName) throws Exception{
fileName=request.getParameter("fileName");
fileName=new String(fileName.getBytes("ISO8859-1"),"UTF-8");
//把处理好的文件名赋值
this.fileName=fileName;
}

//2.下载提交的方法(在struts.xml中配置返回stream)
public String download() throws Exception{
String fileName=request.getParameter("fileName");
/*fileName=new String(fileName.getBytes("ISO8859-1"),"UTF-8");
fileName=URLEncoder.encode(fileName,"UTF-8");*/
this.fileName=fileName;
return "download";
}

//3.返回流的方法
public InputStream getAttrInputStream(){
return ServletActionContext.getServletContext().getResourceAsStream("/WEB-INF/download/"+fileName);
}
//4.下载显示的中文名,(浏览器显示的文件名)
public String getDownFileName(){
try{
fileName=URLEncoder.encode(fileName,"UTF-8");
}catch(Exception e){
throw new RuntimeException();
}
return fileName;
}
}

struts.xml配置:

使用struts2实现文件上传与下载功能

jsp代码:

使用struts2实现文件上传与下载功能

依然存在的问题:

(1)上传图片成功后,想直接让图片显示出来,但是一直有问题,

  第一个问题是,如果上传的文件保存到绝对路径时,会直接保存到tomcat中的webapps的对应目录下,不知道怎么取出显示出来

  第二个问题是,如果我选择保存到相对路径,必须要点两次上传按钮才能显示

相关文章