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

文件上传:

一开始我在网上找到基于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实现文件上传与下载功能的更多相关文章

  1. 使用SpringMVC框架实现文件上传和下载功能

    使用SpringMVC框架实现文件上传和下载功能 (一)单个文件上传 ①配置文件上传解释器 <!—配置文件上传解释器 --> <mvc:annotation-driven>&l ...

  2. JavaWeb中的文件上传和下载功能的实现

    导入相关支持jar包:commons-fileupload.jar,commons-io.jar 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上 ...

  3. struts2实现文件上传和下载

    在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来 ...

  4. Struts2的文件上传与下载

    <一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...

  5. 【Struts2】文件上传与下载

    一.上传 1.1 Struts2实现步骤 浏览器端 服务器端 1.2 关于Struts2中文件上传细节: 1.3 示例 jsp文件 Action类 struts.xml文件配置 二.下载 2.1 文件 ...

  6. (十五)struts2的文件上传和下载

    文件上传的原理 我们以前学习上传的时候知道需要将表单的enctype属性设置为multipart/form-data. 表单的enctype属性指定的是表单数据的编码方式,有三个值: -applica ...

  7. Struts2之文件上传与下载

    时间:2017-1-11 15:47 --使用commons-fileupload组件上传1.客户端    *   method="post"    *   <input t ...

  8. 11、Struts2 的文件上传和下载

    文件上传 表单准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设置 ...

  9. Struts2入门(七)——Struts2的文件上传和下载

    一.前言 在之前的随笔之中,我们已经了解Java通过上传组件来实现上传和下载,这次我们来了解Struts2的上传和下载. 注意:文件上传时,我们需要将表单提交方式设置为"POST" ...

随机推荐

  1. 图论1 Tarjan算法

    强连通分量 模板(强联通分量个数+缩点) #include<iostream> #include<cstdio> #define MAXn 100000 #define MAX ...

  2. pgbouncer的安装和配置

    tar -zxvf libevent-2.0.21-stable.tar.gzcd libevent-2.0.21-stable/mkdir /home/pg10/libevent./configur ...

  3. 如何在 Laravel 中 “规范” 的开发验证码发送功能

    什么是ThinkSNS ? ThinkSNS(简称TS),一款全平台综合性社交系统,为国内外大中小企业和创业者提供社会化软件研发及技术解决方案,目前最新版本为ThinkSNS+(简称TS+).Thin ...

  4. vue路由的四种传值

    第一种:props 配置: 组件内定义: props: ['id'] 路由映射配置,开启props:true : { path: '/user/:id', component: User, props ...

  5. IDEA | 创建启动SpringBoot项目命令

    clean package spring-boot:run -Dmaven.test.skip=true

  6. CSS样式之操作属性二

    ********css样式之属性操作******** 一.文本属性 1.text-align:cnter 文本居中 2.line heigth 垂直居中 :行高,和高度对应 3.vertical-al ...

  7. dp优化1——sgq(单调队列)

    该文是对dp的提高(并非是dp入门,dp入门者请先参考其他文章) 有时候dp的复杂度也有点大...会被卡. 这几次blog大多数会讲dp优化. 回归noip2017PJT4.(题目可以自己去百度).就 ...

  8. NOI2015软件包管理器 树剖线段树

    题目: 一棵树,兹磁 1.查询根到一个点的染色点数并全染好 2.查询子树内染色点数并把颜色洗掉 树剖裸题,丝毫不虚(为什么我考试的时候碰不到这种好题呢)好像20min写完搞定 #include < ...

  9. java.sql.SQLException: No suitable driver found for jdbc:hive://localhost:10000/default

    error: java.sql.SQLException: No suitable driver found for jdbc:hive://localhost:10000/default at ja ...

  10. (转)Linux Network IO Model、Socket IO Model - select、poll、epoll

    Linux Network IO Model.Socket IO Model - select.poll.epoll  原文:https://www.cnblogs.com/LittleHann/p/ ...