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

文件上传:

一开始我在网上找到基于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. tp5 搜索之后保留分页

    当接收到参数是执行搜索 public function index($name=null){ if (isset($_REQUEST['username'])) { $name = $_REQUEST ...

  2. 51nod1117(简单huffman tree)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117 题意:中文题诶- 思路:简单huffman tree ...

  3. Java 为程序创建日志系统

    使用JAVA创建日志系统有两种方法 1.使用log4j操作日志文件 2.使用系统重定向输出日志信息 方法1:使用log4j操作日志文件(可使用jar或者xml) 步骤1:下载log4j.jar 下载地 ...

  4. Windwos10环境下的Geany的安装与新手使用

    相信学习Python的小伙伴都会接触到Geany这个轻量级的python文本编辑器吧,下面就是我对于安装Geany的一些小经验分享. 1:安装geany很简单,进入geany官网download下载相 ...

  5. iOS 根据文字字数动态确定Label宽高

    iOS7中用以下方法 - (CGSize)sizeWithAttributes:(NSDictionary *)attrs; 替代过时的iOS6中的- (CGSize)sizeWithFont:(UI ...

  6. php输出变量加{}的作用

    之前在输出字符串中有变量如 echo “中间有”; echo $i; echo "变量"; 现在发现一个好方法,把变量用{}括起来 echo "中间有{$i}变量&quo ...

  7. [Java]HashMap实现与哈希冲突,与HashTable的区别

    对于 Map ,最直观就是理解就是键值对,映射,key-value 形式.一个映射不能包含重复的键,一个键只能有一个值.平常我们使用的时候,最常用的无非就是 HashMap. HashMap 实现了 ...

  8. [LOJ 2190] 「SHOI2014」信号增幅仪

    [LOJ 2190] 「SHOI2014」信号增幅仪 链接 链接 题解 坐标系直到 \(x\) 轴与椭圆长轴平行 点的坐标变换用旋转公式就可以了 因为是椭圆,所以所有点横坐标除以 \(p\) 然后最小 ...

  9. (转) cocos 里面scrollView一些方法

    void setBounceEnabled (bool enabled)设置当滚动到边界时,是否内部容器发生弹回(bounce)效果 bool isBounceEnabled () const获取边界 ...

  10. 解决ueditor jquery javascript 取值问题

    代码如下: var content = UE.getEditor('myEditor').getContent();   myEditor是ueditor 的名称name.   代码如下: <t ...