六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 98|回复: 0

struts上传

[复制链接]

升级  94%

13

主题

13

主题

13

主题

童生

Rank: 1

积分
47
 楼主| 发表于 2013-2-7 19:58:45 | 显示全部楼层 |阅读模式
package com.jonsion.struts;


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


public class UploadActionForm extends ActionForm {
private String title;
private FormFile uploadFile;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public FormFile getUploadFile() {
return uploadFile;
}
public void setUploadFile(FormFile uploadFile) {
this.uploadFile = uploadFile;
}
}








package com.jonsion.struts;


import java.io.File;
import java.io.FileOutputStream;


import javax.mail.Folder;
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;


public class UploadTestAction extends Action {


public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UploadActionForm uaf=(UploadActionForm)form;
System.out.println("title="+uaf.getTitle());
FormFile ff=(FormFile)uaf.getUploadFile();
if(ff!=null){
System.out.println("filename="+ff.getFileName());
String filepath="c:\\upload\\";
File folder=new File(filepath);
if(!folder.exists())
folder.mkdirs();
File file=new File(filepath+ff.getFileName());
FileOutputStream fos=new FileOutputStream(file);
fos.write(ff.getFileData());
fos.flush();
}
// TODO Auto-generated method stub
return mapping.findForward("success");
}
}








<?xml version="1.0" encoding="ISO-8859-1" ?>




<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">








<struts-config>
<form-beans>
<form-bean name="uploadForm" type="com.jonsion.struts.UploadActionForm"/>


</form-beans>


<action-mappings>
<action path="/uploadfile" type="com.jonsion.struts.UploadTestAction" name="uploadForm" scope="request">
<forward name="success" path="/upload_success.jsp"/>
</action>
</action-mappings>
<controller maxFileSize="2M"/>
</struts-config>




<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>测试动态ActionForm</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>
   <li>测试动态ActionForm</li>
<hr>
<form action="dynaactionform.do" method="post">
用户名:<input type="text" name="username"><br>
年龄:<input type="text" name="age"><br>
<input type="submit" value="提交">
</form>
<p>
<li>struts上传测试</li>
<hr>
<form action="uploadfile.do" method="post" enctype="multipart/form-data">
标题:<input type="text" name="title"><br>
文件:<input type="file" name="uploadFile"><br>
<input type="submit" value="上传">
</form>
  </body>
</html>




<%@ 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>struts上传测试</title>
</head>
<body>
<h1>struts上传测试</h1>
<hr>
标题:${uploadForm.title }<br>
文件名:${uploadForm.uploadFile.fileName}<br>
</body>
</html>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表