Struts 2 文件上传

来源:互联网 发布:淘宝上尾单衣服 编辑:程序博客网 时间:2024/05/16 06:31
 1. upload 页面
 
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><!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>文件上传!</title></head><body><s:form action="upload" method="POST" enctype="multipart/form-data">    <tr><!-- 上传文件标签定义 --><td>上传文件:<s:file name="file"></s:file></td></tr><tr><td>再次上传文件:<s:file name="file"></s:file></td></tr><tr><td align="left"><s:submit name="submit" value="提交"></s:submit></td></tr></s:form></body></html>


 2. success.jsp

<%@ 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>上传成功</title></head><body>上传成功!</body></html>


3. failure.jssp 页面

<%@ 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>上传失败</title></head><body>上传失败,请退回重新上传!</body></html>


4. java 代码:

package com.infy.jsp;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.List;import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport { private static final long serialVersionUID = -6778082832850671224L; //上传文件存放路径 private final static String UPLOADDIR = "/upload"; //上传文件集合 private List<File> file; //上传文件名集合 private List<String> fileFileName; //上传文件内容类型集合 private List<String> fileContentType; public List<File> getFile() {  return file; } public void setFile(List<File> file) {  this.file = file; } public List<String> getFileFileName() {  return fileFileName; } public void setFileFileName(List<String> fileFileName) {  this.fileFileName = fileFileName; } public List<String> getFileContentType() {  return fileContentType; } public void setFileContentType(List<String> fileContentType) {  this.fileContentType = fileContentType; } public String execute() throws Exception {    //System.out.println("AAAAAAAAAA");    for (int i = 0; i < file.size(); i++) {   //循环上传每个文件   uploadFile(i);  }  return "success"; } //执行上传功能 private void uploadFile(int i) throws FileNotFoundException, IOException {  try {   InputStream in = new FileInputStream(file.get(i));   String dir = "E:\\" +UPLOADDIR;   File uploadFile = new File(dir, this.getFileFileName().get(i));   OutputStream out = new FileOutputStream(uploadFile);   byte[] buffer = new byte[1024 * 1024];   int length;   while ((length = in.read(buffer)) > 0) {    out.write(buffer, 0, length);   }   in.close();   out.close();  } catch (FileNotFoundException ex) {   ex.printStackTrace();  } catch (IOException ex) {   ex.printStackTrace();  } }}


5. struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <package name="main" extends="struts-default">            <!-- 配置FileUploadAction,njsp页面中action标签name的值保持一致 -->        <action name="upload" class="com.infy.jsp.FileUploadAction">            <!-- 配置跳转的url路径,如果是成功"success",可以使用默认不写 -->            <result>success.jsp</result>            <result name="failure">failure.jsp</result>        </action>            </package></struts>


6.web.xml文件

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>StrutsUpload</display-name>  <filter>    <filter-name>struts2</filter-name>    <filter-class>            org.apache.struts2.dispatcher.FilterDispatcher        </filter-class>  </filter>  <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>  <welcome-file-list>    <welcome-file>upload.jsp</welcome-file>  </welcome-file-list></web-app>


 

原创粉丝点击