服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - JavaWeb Struts文件上传功能实现详解

JavaWeb Struts文件上传功能实现详解

2020-05-22 11:22qq_16430735 JAVA教程

这篇文章主要为大家详细介绍了JavaWeb Struts文件上传功能实现过程,思路清晰,供大家参考,感兴趣的小伙伴们可以参考一下

一.Struts文件上传的思路
之前也讲过了FileUpload这个组件,功能很强大,但是操作繁琐复杂。这次,在Strust中也对文件上传有所支持,并FileUpload进行了包装,用起来方便了许多。
这里涉及到了一个标签和一个类:
<html:file property="对应ActionForm中的属性名称">
用这个标签可以上传文件,但是要接收次内容必须依靠org.apache.struts.upload.FormFile
接口完成。

JavaWeb Struts文件上传功能实现详解

二.实现
sh.jsp:

?
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
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
 prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
 prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
 prefix="logic"%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
 
<title>sh.jsp</title>
 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 
</head>
 
<body>
 <html:form action="/jsp/upload.do" method="post" enctype="multipart/form-data">
 
 <html:file property="photo"></html:file>
 <html:submit value="上传"></html:submit>
 
 </html:form>
 
</body>
</html>

UploadForm.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.zyy.struts.form;
 
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
 
public class UploadForm extends ActionForm {
 
 private FormFile photo;
 
 public FormFile getPhoto() {
 return photo;
 }
 
 public void setPhoto(FormFile photo) {
 this.photo = photo;
 }
 
}

IPTimeStamp.java:

?
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
package com.zyy.util;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
 
public class IPTimeStamp {
 private SimpleDateFormat sdf = null;
 private String ip = null;
 
 public IPTimeStamp() {
 }
 
 public IPTimeStamp(String ip) {
 this.ip = ip;
 }
 
 public String getIPTimeRand() {
 StringBuffer buf = new StringBuffer();
 if (this.ip != null) {
  String s[] = this.ip.split("\\.");
  for (int i = 0; i < s.length; i++) {
  buf.append(this.addZero(s[i], 3));
  }
 }
 buf.append(this.getTimeStamp());
 Random r = new Random();
 for (int i = 0; i < 3; i++) {
  buf.append(r.nextInt(10));
 }
 return buf.toString();
 }
 
 public String getDate() {
 this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
 return this.sdf.format(new Date());
 }
 
 public String getTimeStamp() {
 this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
 return this.sdf.format(new Date());
 }
 
 private String addZero(String str, int len) {
 StringBuffer s = new StringBuffer();
 s.append(str);
 while (s.length() < len) {
  s.insert(0, "0");
 }
 return s.toString();
 }
 
 public static void main(String args[]) {
 System.out.println(new IPTimeStamp("192.168.1.1").getIPTimeRand());
 }
}

UploadAction.java:

 

?
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
69
70
package com.zyy.struts.action;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
 
import com.zyy.struts.form.UploadForm;
import com.zyy.util.IPTimeStamp;
 
public class UploadAction extends Action {
 
 public ActionForward execute(ActionMapping mapping, ActionForm form,
  HttpServletRequest request, HttpServletResponse response)
  throws Exception {
 
 UploadForm uploadForm = (UploadForm) form;
 
 IPTimeStamp ips = new IPTimeStamp(request.getRemoteAddr());
 
 // 文件名
 String fileName = ips.getIPTimeRand()
  + "."
  + uploadForm.getPhoto().getFileName().split("\\.")[uploadForm
   .getPhoto().getFileName().split("\\.").length - 1];
 
 // 输出路径
 File outFile = new File(super.getServlet().getServletContext()
  .getRealPath("/")
  + "upload" + File.separator + fileName);
 
 // 存放图片的文件夹
 File file = new File(super.getServlet().getServletContext()
  .getRealPath("/")
  + "upload");
 if (!file.exists()) {
 
  file.mkdir();
 
 }
 
 InputStream input = uploadForm.getPhoto().getInputStream();
 
 OutputStream output = new FileOutputStream(outFile);
 
 byte data[] = new byte[1024];
 
 int temp = 0;
 
 while ((temp = input.read(data, 0, 1024)) != -1) {
 
  output.write(data);
 
 }
 
 output.close();
 input.close();
 
 return null;
 }
 
}

struts-config.xml:

 

?
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
<?xml version="1.0" encoding="ISO-8859-1" ?>
 
 
<!DOCTYPE struts-config PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
     "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
 
 
<struts-config>
 <form-beans>
 
 <form-bean name="uploadForm" type="com.zyy.struts.form.UploadForm"></form-bean>
 
 </form-beans>
 
 <global-exceptions>
 </global-exceptions>
 
 <global-forwards>
 </global-forwards>
 
 <action-mappings>
 
 <action path="/jsp/upload" attribute="uploadForm" input="/jsp/sh.jsp"
  name="uploadForm" scope="request" type="com.zyy.struts.action.UploadAction">
  
 </action>
 
 </action-mappings>
 
 <message-resources parameter="resource.MessageResources" />
</struts-config>

JavaWeb Struts文件上传功能实现详解

由于我保存在的是super.getServlet().getServletContext().getRealPath("/")+ "upload"
这是虚拟目录的真实路径的upload文件夹下。 

JavaWeb Struts文件上传功能实现详解

由此可见,在Struts中的文件上传原理和FileUpload一样,但是Struts进行了包装,所以用起来明显比单独用FileUpload组件方便了许多。

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

延伸 · 阅读

精彩推荐