基於struts的文件上傳支持包:
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar
freemarker-2.3.16.jar
javassist-3.7.ga.jar
ognl-3.0.jar
struts2-convention-plugin-2.2.1.1.jar
struts2-core-2.2.1.1.jar
xwork-core-2.2.1.1.jar
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'upLoadFile.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!--當上傳文件時多個時:後臺名字是存在list集合裏面的,所以取出時要進行遍歷 代碼如下:
<s:property value=""/>取出屬性
//struts標籤遍歷
<s:iterator value="file" id="f">
<s:property value="#f"/>//利用OGNL表達式取出各個元素
</s:iterator>
-->
<form action="fileUpload.action" method="post" enctype="multipart/form-data" >
文件選擇:<input type="file" name="file" value=""/><br>
<input type="submit" value="submit">
</form>
</body>
<!-- -->
</html>
action:
package upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class StrutsUploadAction extends ActionSupport
{
private String username;
private File file;
private String fileFileName;
private String fileContentType;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public File getFile()
{
return file;
}
public void setFile(File file)
{
this.file = file;
}
public String getFileFileName()
{
return fileFileName;
}
public void setFileFileName(String fileFileName)
{
this.fileFileName = fileFileName;
}
public String getFileContentType()
{
return fileContentType;
}
public void setFileContentType(String fileContentType)
{
this.fileContentType = fileContentType;
}
@Override
public String execute() throws Exception
{
String root = ServletActionContext.getRequest().getRealPath("/dom");//文件保存服務器路徑
System.out.println("11111111111111");
InputStream is = new FileInputStream(file);//創建文件輸入流
System.out.println("path: " + file.getAbsolutePath());
System.out.println("file: " + file.getName());
System.out.println("fileFileName: " + fileFileName);
File destFile = new File(root, fileFileName);//文件的保存路徑和文件名字
OutputStream os = new FileOutputStream(destFile);//創建文件輸出流
byte[] buffer = new byte[400];
int length = 0;
while(-1 != (length = is.read(buffer)))
{
os.write(buffer, 0, length);
Thread.sleep(1000);
}
is.close();
os.close();
return SUCCESS;
}
/*
* 如果上傳多個文件,則定義List<File> file;屬性
* List<String> fileFileName;
* List<String> fileContentType
* 還是眼愛的名字 固定不變,
* 在list循環裏面創建文件遍歷輸入和輸出流
* 下面創建輸入流InputStream is = new FileInputStream(file.get(i));//創建文件輸入流
* 同理下面的操作
*/
}