使用struts2实现文件上传与下载功能
这个问题做了两天,在网上找了很多例子,但是还有一些功能没有实现,暂时先把代码贴出来,以后在做这方面的功能时在修改
文件上传:
一开始我在网上找到基于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配置:
jsp页面代码:
文件下载代码:
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配置:
jsp代码:
依然存在的问题:
(1)上传图片成功后,想直接让图片显示出来,但是一直有问题,
第一个问题是,如果上传的文件保存到绝对路径时,会直接保存到tomcat中的webapps的对应目录下,不知道怎么取出显示出来
第二个问题是,如果我选择保存到相对路径,必须要点两次上传按钮才能显示
使用struts2实现文件上传与下载功能的更多相关文章
- 使用SpringMVC框架实现文件上传和下载功能
使用SpringMVC框架实现文件上传和下载功能 (一)单个文件上传 ①配置文件上传解释器 <!—配置文件上传解释器 --> <mvc:annotation-driven>&l ...
- JavaWeb中的文件上传和下载功能的实现
导入相关支持jar包:commons-fileupload.jar,commons-io.jar 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上 ...
- struts2实现文件上传和下载
在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来 ...
- Struts2的文件上传与下载
<一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...
- 【Struts2】文件上传与下载
一.上传 1.1 Struts2实现步骤 浏览器端 服务器端 1.2 关于Struts2中文件上传细节: 1.3 示例 jsp文件 Action类 struts.xml文件配置 二.下载 2.1 文件 ...
- (十五)struts2的文件上传和下载
文件上传的原理 我们以前学习上传的时候知道需要将表单的enctype属性设置为multipart/form-data. 表单的enctype属性指定的是表单数据的编码方式,有三个值: -applica ...
- Struts2之文件上传与下载
时间:2017-1-11 15:47 --使用commons-fileupload组件上传1.客户端 * method="post" * <input t ...
- 11、Struts2 的文件上传和下载
文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...
- Struts2入门(七)——Struts2的文件上传和下载
一.前言 在之前的随笔之中,我们已经了解Java通过上传组件来实现上传和下载,这次我们来了解Struts2的上传和下载. 注意:文件上传时,我们需要将表单提交方式设置为"POST" ...
随机推荐
- UE4中资源加载资源的方式
在UNITY中,我们加载资源一般是通过Resources.Load(path).即可完成.该方法返回的是Object类型.如果你想要的是材质或者贴图等等,只要价格类型转换的关键字就可以了例如 as M ...
- DRF教程8-过滤
在写后端api时,经常需要使用各种过滤条件,可以使用Q对查询集进行过滤,这里介绍一个新玩意儿 以下是基础文档 https://django-filter.readthedocs.io/en/maste ...
- vim如何删除行首、行位空格、空格行
删除空格行: 非编辑状态下输入:g/^$/d 删除行首空格: 非编辑状态下输入:%s/^\s*//g 删除行尾空格: 非编辑状态下输入:%s/\s*$//g
- shell中变量内容的删除,替代
删除 ${varname#strMatch} // 在varname中从头匹配strMatch,然后删除从头到第一次匹配到的位置 ${varname##strMatch} // 在varname中从头 ...
- 牛客练习赛41E(球的体积并)
球冠公式是\(\frac{\pi h^2(3R-h)}{3}\),这样再余弦公式用\(R_a\)和\(R_b\)导一导两个球冠的\(h\)就做完了.算是补了个camp时没做出来的小坑了. #inclu ...
- bzoj4650: [Noi2016]优秀的拆分 hash
好气啊,没开longlong又biubiu了 底层: 用hash或者奇奇怪怪的算法兹磁logn求最长公共前后缀 思路: 统计出从一个点开始和结束的形如AA的子串的个数 统计的时候把相邻的结果相乘加起来 ...
- Codeforces Round #431 (Div. 2) A
Where do odds begin, and where do they end? Where does hope emerge, and will they ever break? Given ...
- Linux 运维培训笔记
2018/01/05 权限管理:sudoers文件 KAIFA_ADMINS ALL=(OP1) KAIFACMD 用户(大写) ...
- (转)io优化
原文:http://blog.csdn.net/gzh0222/article/details/9227393 1.系统学习 IO性能对于一个系统的影响是至关重要的.一个系统经过多项优化以后,瓶颈往往 ...
- SyntaxHighlighter
SyntaxHighlighter uses separate syntax files called brushes to define its highlighting functionality ...