Struts2文件上传
来源:互联网 发布:mac终端如何切换用户 编辑:程序博客网 时间:2024/06/16 12:38
Struts2文件上传:
上传页面:index.jsp
<%@ 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>文件上传测试</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><p>文件上传</p><form action="fileAction" method="post" enctype="multipart/form-data" name="form1"> <p>标题: <input type="text" name="title" id="title"></p> <p>文件: <input type="file" name="process" id="process"></p> <p> <input type="submit" name="button" id="button" value="提交"> </p></form><p> <br></p></body></html>
对应后台接收类:
package com.model.dto;import java.io.File;public class FileModel {private String title = null;private File process = null;private String uploadContentType = null;private String savePath = null;public String getUploadContentType() {return uploadContentType;}public void setUploadContentType(String uploadContentType) {this.uploadContentType = uploadContentType;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public File getProcess() {return process;}public void setProcess(File process) {this.process = process;}public String getSavePath() {return savePath;}public void setSavePath(String savePath) {this.savePath = savePath;}}
处理的Action:
package com.action;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import org.apache.struts2.ServletActionContext;import com.model.dto.FileModel;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class FileAction extends ActionSupport implements ModelDriven<FileModel>{private FileModel fileModel = new FileModel();public String execute(){System.out.println(fileModel.getTitle());System.out.println(fileModel.getSavePath());System.out.println(fileModel.getProcess());try {File f = new File(ServletActionContext.getRequest().getRealPath(fileModel.getSavePath()));if(!f.exists()){f.mkdirs();}FileOutputStream fos = new FileOutputStream(f+"/1.txt");FileInputStream fis = new FileInputStream(fileModel.getProcess());byte [] b = new byte[1024];while((fis.read(b))!=-1){fos.write(b, 0, b.length);}fos.close();fis.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return SUCCESS;}public FileModel getModel() {return fileModel;}}
Action的配置文件:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="fileAction" class="com.action.FileAction" > <param name="savePath">/upload</param> <result> /success.jsp </result> </action> </package> <!-- Add packages here --></struts>
- struts2上传文件(三) struts2上传文件
- [Struts2]Struts2的文件上传
- Struts2的文件上传
- Struts2文件上传
- Struts2 上传文件实例
- struts2文件上传
- struts2文件上传
- Struts2的文件上传
- struts2-上传文件(1)
- struts2-上传文件(2)
- Struts2上传文件示例
- struts2文件上传
- Struts2实现文件上传
- struts2文件上传
- Struts2 文件上传
- ext + struts2 上传文件
- Struts2 文件上传
- Struts2的文件上传
- SrpingMVC 返回JSON
- 我心目中的编程高手(完整版)
- eclipse CDT Unresolved inclusion 错误解决办法
- Ext Scheduler Web甘特图控件
- Javascript获取url里面的一些东西
- Struts2文件上传
- apache+mod_wsgi+django在windows下的部署
- Ubuntu(Linux)使用Eclipse搭建C/C++编译环境
- sql server 脚本 游标的使用
- User Agent List
- maximo7邮件设置
- web的工作流设计器
- ubuntu下配置jdk+eclipse+android sdk
- CMS,BBS及内容管理系统