1、jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>struts 2 File Upload Test</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> <s:form action="fileUpload" method="POST" enctype="multipart/form-data"> <s:file name="myFile" label="文件上传"/><br/> <s:textfield name="inputText" label="输入文本"/><br/> <s:submit value="提交" /> </s:form> </body> </html>
2、Action类
package com.jxlg.demo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport { private static final int BUFFER_SIZE = 16 * 1024; private String savePath; // 保存路径 private File myFile; // 待上传文件 private String myFileFileName; // 文件名 public String getMyFileFileName() { return myFileFileName; } public void setMyFileFileName(String myFileFileName) { this.myFileFileName = myFileFileName; } private String inputText; public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } public File getMyFile() { return myFile; } public void setMyFile(File myFile) { this.myFile = myFile; } public void setInputText(String inputText) { this.inputText = inputText; } public String getInputText() { return inputText; } private void copyFile(File src, File savePath) { try { // 原文件 FileInputStream fis = new FileInputStream(src); BufferedInputStream bis = new BufferedInputStream(fis); // 文件输出保存 FileOutputStream fos = new FileOutputStream(savePath); BufferedOutputStream bos = new BufferedOutputStream(fos); byte buffer[] = new byte[BUFFER_SIZE]; while (fis.read(buffer) > 0) { bos.write(buffer); } System.out.println("成功上传文件(临时文件)" + src.getName()); System.out.println("文件存放在:" + savePath.getAbsolutePath()); bos.close(); fos.close(); bis.close(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println("文本输入框输入的文字是:" + getInputText()); File src = getMyFile(); Date date = new Date(); System.out.println("=========filename:" + getMyFileFileName() + "=======contentType:" + getExtendsion()); //可以设置savePath的值,可以设置服务器的路径 File saveFilePath = new File(savePath + date.getTime() + getExtendsion()); System.out.println(saveFilePath.getAbsolutePath()); copyFile(src, saveFilePath); return super.execute(); } public String getExtendsion() { int pos = getMyFileFileName().indexOf("."); return getMyFileFileName().substring(pos); } }
3、配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.devMode" value="true"></constant> <package name="mypackage" extends="struts-default"> <action name="fileUpload" class="com.jxlg.demo.FileUploadAction"> <interceptor-ref name="fileUpload"> <param name="allowedType">image/bmp,image/png,image/gif,image/jpeg</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> <param name="savePath">d:\\helloworld\\</param> <result name="success">/success_upload.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts>
5、附加的success_upload.jsp