读书人

关于参数的有关问题

发布时间: 2013-07-01 12:33:04 作者: rapoo

关于参数的问题


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title>文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<form action="${pageContext.request.contextPath}/upload2/upload2.do" enctype="multipart/form-data" method="post">
文件:<input type="file" name="image">
<input type="submit" value="上传" />
</form>
<br/>
<s:fielderror />
</body>
</html>



package com.ljq.action;

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UploadAction extends ActionSupport {
private File image; // 上传的文件
private String imageFileName; // 文件名称
private String imageContentType; // 文件类型

public String execute() throws Exception {
String realpath = ServletActionContext.getServletContext().getRealPath(
"/excel");
// D:\apache-tomcat-6.0.18\webapps\struts2_upload\images
System.out.println("realpath: " + realpath);
if (image != null) {
File savefile = new File(new File(realpath), imageFileName);
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
ActionContext.getContext().put("message", "文件上传成功");
}
return "success";
}

/**
* @return the image
*/
public File getImage() {
return image;
}

/**
* @param image the image to set
*/
public void setImage(File image) {
this.image = image;
}

/**
* @return the imageFileName
*/
public String getImageFileName() {
return imageFileName;
}

/**
* @param imageFileName the imageFileName to set
*/
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}

/**
* @return the imageContentType
*/
public String getImageContentType() {


return imageContentType;
}

/**
* @param imageContentType the imageContentType to set
*/
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}

}




是关于文件上传的
在action定义了3个参数 一个是文件 一个是类型 一个是文件名
但是在前面的jsp页面只能看到image这个参数
请问imageFileName 跟 imageContentType是怎么来的?
[解决办法]
image是一个文件。
jsp页面上image的type类型是file,那么就会上传三个参数,分别是文件,文件名和文件类型。
imageFileName代表文件名。需要提供set方法
imageContentType代表文件类型。需要提供set方法
[解决办法]
imageFileName 跟 imageContentType是根据set方法,然后程序赋值的

读书人网 >J2EE开发

热点推荐