JavaWeb Struts文件上傳功能實(shí)現(xiàn)詳解
一.Struts文件上傳的思路
之前也講過(guò)了FileUpload這個(gè)組件,功能很強(qiáng)大,但是操作繁瑣復(fù)雜。這次,在Strust中也對(duì)文件上傳有所支持,并FileUpload進(jìn)行了包裝,用起來(lái)方便了許多。
這里涉及到了一個(gè)標(biāo)簽和一個(gè)類(lèi):
<html:file property="對(duì)應(yīng)ActionForm中的屬性名稱(chēng)">
用這個(gè)標(biāo)簽可以上傳文件,但是要接收次內(nèi)容必須依靠org.apache.struts.upload.FormFile
接口完成。

二.實(shí)現(xiàn)
sh.jsp:
<%@ 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:
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:
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:
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:
<?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>

由于我保存在的是super.getServlet().getServletContext().getRealPath("/")+ "upload"
這是虛擬目錄的真實(shí)路徑的upload文件夾下。

由此可見(jiàn),在Struts中的文件上傳原理和FileUpload一樣,但是Struts進(jìn)行了包裝,所以用起來(lái)明顯比單獨(dú)用FileUpload組件方便了許多。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式
- struts2中實(shí)現(xiàn)多個(gè)文件同時(shí)上傳代碼
- 關(guān)于Struts2文件上傳與自定義攔截器
- java中struts2實(shí)現(xiàn)簡(jiǎn)單的文件上傳與下載
- JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- 詳解Java的Struts框架中上傳文件和客戶(hù)端驗(yàn)證的實(shí)現(xiàn)
- java中struts2實(shí)現(xiàn)文件上傳下載功能
- java中Struts2 的文件上傳和下載示例
- struts2實(shí)現(xiàn)多文件上傳的示例代碼
相關(guān)文章
面試題:java中為什么foreach中不允許對(duì)元素進(jìn)行add和remove
讀者遇到了一個(gè)比較經(jīng)典的面試題,也就是標(biāo)題上說(shuō)的,為什么 foreach 中不允許對(duì)元素進(jìn)行 add 和 remove,本文就詳細(xì)的介紹一下,感興趣的可以了解一下2021-10-10
springboot整合Nginx實(shí)現(xiàn)負(fù)載均衡反向代理的方法詳解
這篇文章主要給大家介紹了關(guān)于springboot整合Nginx實(shí)現(xiàn)負(fù)載均衡反向代理的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
Java Set集合及其子類(lèi)HashSet與LinkedHashSet詳解
這篇文章主要介紹了Java Set集合及其子類(lèi)HashSet與LinkedHashSet詳解,文章通過(guò)Set集合存儲(chǔ)原理展開(kāi)文章主題相關(guān)介紹,感興趣的小伙伴可以參考一下2022-06-06
Maven+oracle+SSM搭建簡(jiǎn)單項(xiàng)目的方法
本篇文章主要介紹了Maven+oracle+SSM搭建簡(jiǎn)單項(xiàng)目的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
解決idea使用過(guò)程中讓你覺(jué)得不爽的一些問(wèn)題(小結(jié))
這篇文章主要介紹了解決idea使用過(guò)程中讓你覺(jué)得不爽的一些問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
java之向linux文件夾下寫(xiě)文件無(wú)權(quán)限的問(wèn)題
這篇文章主要介紹了java之向linux文件夾下寫(xiě)文件無(wú)權(quán)限的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
解析Spring RestTemplate必須搭配MultiValueMap的理由
本文給大家介紹Spring RestTemplate必須搭配MultiValueMap的理由,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-11-11

