您的位置:首页 > 编程语言 > Java开发

struts2文件上传笔记-huangshl

2013-09-07 16:48 399 查看
---恢复内容开始---

实现struts2文件上传首先确定form表单的enctype的属性,该属性设置就是决定表单数据的编码方式,一般有三种方式:

application/x-www-form-urlencoded,默认的,只处理表单的value属性,采用这种编码方式会将表单的值处理成URI编码方式,

这种方式上传不了文件。

要想上传文件要需要使用含有文件域的编码方式(enctype="multipart/form-data"),这种方式以二进制的方式处理表单数据,

把文件域指定的文件内容封装到参数里-一般上传文件采用这种编码方式!

text/plain:这种编码方式当表单的action属性为mailto:URL的形式时比较方便,这种方式主要用于直接通过表单发送邮件的方式。(这种方式已经不再采用了)速速

表单代码:

<%@ 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>Insert title here</title>
</head>
<body>
<s:fielderror>图书资料上传</s:fielderror>
<s:form action="singFile" namespace="/luo" method="post" enctype="multipart/form-data">
<s:textfield name="bookName" label="书名"></s:textfield>
<s:textfield name="autor" label="作者"></s:textfield>
<s:file name="upload" label="选择文件"></s:file>
<s:submit value="提交上传"></s:submit>
</s:form>
</body>
</html>


package com.upload.action;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class SingleFileUpload extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;
private File upload;
private String uploadFileName;
private String uploadContentType;
private String bookName;
private String autor;

public String upload(){
String path=ServletActionContext.getServletContext().getRealPath("/upload");
File file=new File(path);
if(!file.exists()){
file.mkdirs();
}
try{
FileUtils.copyFile(upload,new File(file, uploadFileName));

}catch(IOException e){
e.printStackTrace();
}
return "success";
}

public File getUpload() {
return upload;
}

public void setUpload(File upload) {
this.upload = upload;
}

public String getUploadFileName() {
return uploadFileName;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

public String getUploadContentType() {
return uploadContentType;
}

public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public String getAutor() {
return autor;
}

public void setAutor(String autor) {
this.autor = autor;
}

}


<?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>
<constant name="struts.multipart.maxSize" value="104857600"></constant>
<package name="singFileupload" extends="struts-default" namespace="/luo">
<action name="singFile" class="com.upload.action.SingleFileUpload" method="upload">

<result name="success">/success.jsp</result>

</action>

</package>
</struts>


---恢复内容结束---
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: