首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts1下传文件

2012-09-19 
Struts1上传文件//上传界面的代码:%@ page languagejava importjava.util.* pageEncodingutf-8%

Struts1上传文件

//上传界面的代码:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html:html>
?<head>
??<title>Struts上传文件</title>
?</head>
?<body>
??<!-- 这里需要注意的是method一定要设成post,还要设置enctype为multipart/form-data-->
??<html:form action="upload" enctype="multipart/form-data" method="post">
???<table>
????<tr>
?????<td>
??????<div>
???????<html:file property="file" size="30"></html:file>
??????</div>
?????</td>
????</tr>
????<tr>
?????<td>
??????<div>
???????<html:submit property="upload" value="上传"></html:submit>
??????</div>
?????</td>
????</tr>
???</table>
??</html:form>
?</body>
</html:html>

?

//然后是页面对应的ActionForm
package forms;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class UploadForm extends ActionForm {
?private String filename;
?private FormFile file;
?public FormFile getFile() {
??return file;
?}
?public void setFile(FormFile file) {
??this.file = file;
?}
?public String getFilename() {
??return filename;
?}
?public void setFilename(String filename) {
??this.filename = filename;
?}
?
}


//处理业务的Action

package actions;

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 org.apache.struts.upload.FormFile;

import forms.UploadForm;

public class UploadAction extends Action {
?@Override
?public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
??UploadForm uf=(UploadForm)form;
??String filename=uf.getFilename();
??FormFile file=uf.getFile();
??if(file!=null){
???System.out.println("我叫"+file.getFileName());
??}
??//这里需要注意的是request.getRealPath();已经废弃,可以通过
??????????????? //下面的方法来替代
??String path=req.getSession().getServletContext().getRealPath("/");
??InputStream in=file.getInputStream();
??OutputStream out=new FileOutputStream(path+"/upload/"+file.getFileName());
??int read=0;
??byte[] buffer=new byte[1024];
??while((read=in.read(buffer,0,1024))!=-1){
???out.write(buffer, 0, read);
??}
??in.close();
??out.close();
??return mapping.findForward("uploadsuc");
?}
}


//Struts-config.xml的配置
?<form-beans>
??<form-bean name="uploadform" type="forms.UploadForm"></form-bean>
?</form-beans>
?<action path="/upload"
??type="actions.UploadAction"
??name="uploadform"
??input="/test/upload.jsp"
??scope="request"
??>
???<forward name="uploadsuc" path="/test/uploadsuc.jsp"/>
?</action>

热点排行