struts2上传文件
来源:互联网 发布:ubuntu apt get glib 编辑:程序博客网 时间:2024/05/01 23:54
Action文件:
package jp.co.ricoh.action.upload;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.util.Date;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport {/** * */private static final long serialVersionUID = 7944571812664822722L;private static final int BUFFER_SIZE = 16 * 1024;private File file;private String contentType;private String fileName;private String imageFileName;private String caption;public File getFile() {return file;}public void setFile(File file) {this.file = file;}public String getFileContentType() {return contentType;}public void setFileContentType(String contentType) {this.contentType = contentType;}public String getFileFileName() {return fileName;}public void setFileFileName(String fileName) {this.fileName = fileName;}public String getImageFileName() {return imageFileName;}public void setImageFileName(String imageFileName) {this.imageFileName = imageFileName;}public String getCaption() {return caption;}public void setCaption(String caption) {this.caption = caption;}public static int getBufferSize() {return BUFFER_SIZE;}private static void copyFile(File src, File dest) {try {InputStream in = null;OutputStream out = null;try {in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE);out = new BufferedOutputStream(new FileOutputStream(dest),BUFFER_SIZE);byte[] buffer = new byte[BUFFER_SIZE];while (in.read(buffer) > 0) {out.write(buffer);}} finally {if (null != in) {in.close();}if (null != out) {out.close();}}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}private static String getExtention(String fileName) {int pos = fileName.lastIndexOf(".");return fileName.substring(pos);}@Overridepublic String execute() throws Exception {imageFileName = new Date().getTime() + getExtention(fileName);System.out.println(ServletActionContext.getServletContext().getRealPath("/uploadFile"));File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/uploadFile") + "/" + imageFileName);System.out.println(imageFile.getName());copyFile(file, imageFile);return super.execute();}}
struts配置文件:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts><package name="upload" namespace="/" extends="struts-default"><action name="FileUploadAction" class="jp.co.ricoh.action.upload.FileUploadAction"><interceptor-ref name="fileUpload"><param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref><result name="input">/upload/upload.jsp</result><result name="success">/upload/showUpload.jsp</result></action></package></struts>
JSP页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ taglib prefix="s" uri="/struts-tags" %> <!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>Struts2 File Upload</title></head><body><s:fielderror></s:fielderror><s:form action="FileUploadAction" method="post" enctype="multipart/form-data"><s:file name="file" label="Image File"></s:file><s:textfield name="caption" label="Caption"></s:textfield><s:submit></s:submit></s:form></body></html>
- struts2上传文件(三) struts2上传文件
- [Struts2]Struts2的文件上传
- Struts2的文件上传
- Struts2文件上传
- Struts2 上传文件实例
- struts2文件上传
- struts2文件上传
- Struts2的文件上传
- struts2-上传文件(1)
- struts2-上传文件(2)
- Struts2上传文件示例
- struts2文件上传
- Struts2实现文件上传
- struts2文件上传
- Struts2 文件上传
- ext + struts2 上传文件
- Struts2 文件上传
- Struts2的文件上传
- 8、Linux程序设计入门--网络编程
- Android图片显示ICON——基础编
- poj2590
- 9、Linux下C开发工具介绍
- 掐住吃内存大虫的喉咙---find.exe和cmd.exe和Rar.exe病毒
- struts2上传文件
- 黑马程序员 SQL SERVER 基础
- Objective-C中的meta-class
- 将输入中的制表符替换成适当数目的空格,使空格充满到下一个制表符终止的地方
- 将空格串替换为最小数量的制表符和空格,但要保持单词之间的间隔不变。
- 《The C Programming Language》 1-22
- 复制xcode4工程
- 【个人日志】20120318 坚持就是一辈子
- zoj 1334 Basically Speaking