服务器之家

服务器之家 > 正文

Struts2实现上传单个文件功能

时间:2020-11-05 16:10     来源/作者:我扶奶奶过哈登

upload.jsp 这个页面选择提交文件,提交到uploadImage.action

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
  <form action="uploadImage.action" enctype="multipart/form-data" method="post">
    please select the file:<input type="file" name="upload">
    <input type="submit" value="上传文件">
  </form>
</body>
</html>

FileUploadAction.java 将传来的file进行处理

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package action;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class FileUploadAction extends ActionSupport{
  private File upload;
  private String uploadFileName;
  private String uploadContentType;
 
 
  public File getUpload() {
    return upload;
  }
 
 
  public void setUpload(File upload) {
    this.upload = upload;
  }
 
 
  public String getUploadFileName() {
    return uploadFileName;
  }
 
 
  public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
  }
 
 
  public String getUploadContentType() {
    return uploadContentType;
  }
 
 
  public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
  }
 
 
  public String execute(){
    System.out.println(upload);
    System.out.println(uploadContentType);
    System.out.println(uploadFileName);
 
    String savePath = ServletActionContext.getServletContext().getRealPath("/upload/"+this.uploadFileName);
    System.out.println(savePath);
 
    try{
      FileInputStream fis = new FileInputStream(upload);
      FileOutputStream fos = new FileOutputStream(savePath);
      IOUtils.copy(fis, fos);
      fos.flush();
      fos.close();
      fis.close();
    }catch(Exception e){
      e.printStackTrace();
    }
    return "success";
  }
}

uploadFileName和uploadContentType,这两个属性分别用于封装上传文件的文件名、上传文件的文件类型

Struts.xml配置

?
1
2
3
4
<action name="uploadImage" class="action.FileUploadAction">
 <result name="success">uploadSuccess.jsp</result>
 <result name="input">uploadError.jsp</result>
</action>

成功失败界面随便写一个就行了,不贴了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部