<%@ 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是怎么来的?
解决方案 »
- Java 测试题 求解答 谢谢!!
- Excel 导出文件乱码问题,时好时坏
- 【求助】 hibernate 的查询问题
- core java和thinking in java,作为有点C语言基础的处学者应该使用哪本学习?
- 高薪诚聘
- 急救~~~~~~~~~~resin3.0.3+sqlserver 配置JDBC出错,,帮我看下高手们
- 这个符号代表什么意思?
- cmp的问题
- Jbuilde8+WebLogic7配置时要一个名为registry.xml文件,不知哪里可以找到?或有相关资料请寄:[email protected]谢谢。
- 关于<s:debug/>
- struts-2.3.12 给action传参数问题
- myeclipse 普通web项目怎么转成maven web
jsp页面上image的type类型是file,那么就会上传三个参数,分别是文件,文件名和文件类型。
imageFileName代表文件名。需要提供set方法
imageContentType代表文件类型。需要提供set方法