Action:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.ServletContext;
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 org.apache.struts.upload.FormFile;
import com.yourcompany.struts.form.UploadForm;
public class UploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
UploadForm uploadForm = (UploadForm) form;// TODO Auto-generated method stub
FormFile myfile=uploadForm.getMyfile();
/*
//得到文件的基本信息
String type=myfile.getContentType();//得到文件类型
String name=myfile.getFileName();//得到文件名称,如果不选而提交,名称为空字符串
int size=myfile.getFileSize();//得到文件大小
System.out.println(type);
System.out.println(name);
System.out.println(size);
*/
//保存文件在服务器段
FileOutputStream fos=null;
try {
byte[] data=myfile.getFileData();//得到文件数据
String fileName=myfile.getFileName();
System.out.println(this.getServlet().getServletContext().getRealPath("/"));
//Java如果要访问服务器硬盘,一定要提供逻辑路径(硬盘上的路径c:\..)
//怎样将URL相对路径转换成硬盘上的绝对路径?用application
ServletContext application=this.getServlet().getServletContext();
String realPath=application.getRealPath("/Files/");
fos=new FileOutputStream(realPath+"/"+fileName);
fos.write(data);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
try {
InputStream is=myfile.getInputStream();//得到文件的输入流,可以对文件进行分析
BufferedReader br=new BufferedReader(new InputStreamReader(is));//通过BufferedReader来分析文件
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
return new ActionForward("/upload.jsp");
}
}
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for UploadForm form</title>
</head>
<body>
<html:form action="/upload" method="post" enctype="multipart/form-data">
请你选择一个文件惊醒上传:<html:file property="myfile"></html:file>
<html:submit value="上传"/><html:cancel/>
</html:form>
</body>
</html>
多文件上传:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
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 org.apache.struts.upload.FormFile;
import com.yourcompany.struts.form.UploadManyFlesForm;
public class UploadManyFlesAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
UploadManyFlesForm uploadManyFlesForm = (UploadManyFlesForm) form;// TODO Auto-generated method stub
System.out.println("得到myfiles");
ArrayList myfiles=uploadManyFlesForm.getMyfiles();
for(int a=0;a<myfiles.size();a++){
System.out.println(((FormFile)myfiles.get(a)).getFileName());
}
// FileOutputStream fos=null;
// try {
// byte msg[]=myfiles.getFileData();
// String dizhi=this.getServlet().getServletContext().getRealPath("/");
// fos=new FileOutputStream(dizhi+"/"+myfiles.getFileName());
// fos.write(msg);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }finally{
// try {
// fos.close();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
return new ActionForward("/uploadManyFles.jsp");
}
}
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for UploadManyFlesForm form</title>
</head>
<body>
<html:form action="/uploadManyFles" method="post" enctype="multipart/form-data">
请你选择一个文件进行上传:<html:file property="myfile[0]"></html:file><br>
请你选择一个文件进行上传:<html:file property="myfile[1]"></html:file><br>
请你选择一个文件进行上传:<html:file property="myfile[2]"></html:file><br>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>