ITeye博客

struts上传

jimb · 2008-11-13 16:36 · java技术 · Markdown

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"); } }

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<title>测试动态ActionForm</title>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。