主题:struts架构下的文件上传中的jsp参数传递问题。
我在struts架构下写了一个文件上传的小例子。
但是试图在jsp中通过request.setAttribute()方法,将存储路径作为参数传递给action,但是在action中使用request.getAttribute()得到的值为null.这是为什么呢?不能这样做么?
下面是我的代码:
1。up.jsp
-------------------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传表单</title>
</head>
<body>
<html:form action="/upload" parameter="file_path" enctype="multipart/form-data">
[color=FF0000]<%
String file_path = "file_path";
request.setAttribute("file_path",file_path);
%>[/color]
上传文件:
<html:file property="uploadFile"></html:file>
<html:submit value="上传文件"></html:submit>
</html:form>
</body>
</html>
-------------------------------------------------------------------------------------
2.FileUploadForm.java
-------------------------------------------------------------------------------------
package com.upload.struts;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
/**
* 基于Struts FormFile的文件上传.
*/
public class FileUploadForm extends ActionForm{
/**
* 上传的文件
*/
private FormFile uploadFile;
public FormFile getUploadFile() {
return uploadFile;
}
public void setUploadFile(FormFile uploadFile) {
this.uploadFile = uploadFile;
}
}
-------------------------------------------------------------------------------------
3.FileUploadAction.java
-------------------------------------------------------------------------------------
package com.upload.struts;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
/**
* 文件上传的Action,该类用于处理FormFile及将上传的文件保存到磁盘的指定位置.
*/
public class FileUploadAction extends Action {
/**
* 上传的文件保存在服务器的路径
*/
private String UPLOAD_FILE_PATH;
private String uploadfilePath;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
/* System.out.println("----------"+(String)request.getAttribute("file_path")+ "----------");
System.out.println("----------"+ request.getAttribute("file_path") + "----------");*/
[color=FF00FF]this.UPLOAD_FILE_PATH = request.getRealPath("/")+request.getAttribute("file_path") +"\\";[/color]
File targetFolder = new File(UPLOAD_FILE_PATH);
if (targetFolder.isFile() || !targetFolder.exists()) {
targetFolder.mkdir();
}
FileUploadForm uploadForm = (FileUploadForm)form;
//得到上传的文件
FormFile uploadFile = uploadForm.getUploadFile();
//得到文件名
String fileName = uploadFile.getFileName();
//得到文件大小
int fileSize = uploadFile.getFileSize();
//文件重命名保证唯一性,通过在文件名前加入时间前缀来实现。
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
java.util.Date mydate=new java.util.Date();
fileName = sdf.format(mydate) + "_" + fileName;
uploadfilePath=this.UPLOAD_FILE_PATH + fileName;
boolean result = true;
//文件最大限额
int fileMaxSize = 100 * 1024 * 1024;
if (fileSize > fileMaxSize) {
System.out.println("文件超过规定大小!!");
result = false;
}else{
try{
//得到文件的输入流
InputStream is = uploadFile.getInputStream();
//上传文件
uploadFile(fileName,is);
System.out.println("FileName = " + fileName);
System.out.println("FileSize= " + fileSize);
System.out.println("FilePath= " + uploadfilePath);
}catch(IOException ex){
ex.printStackTrace();
//假如上传文件失败,设置一个失败的标记位
result = false;
}
}
//页面跳转。
if(result){
request.setAttribute("size", fileSize);
request.setAttribute("fileName", fileName);
return mapping.findForward("success");
} else {
return mapping.findForward("fail");
}
}
/**
* 上传文件
* @param fileName
* @param is
* @throws IOException
*/
private void uploadFile(String fileName,InputStream is) throws IOException{
OutputStream os = new FileOutputStream(UPLOAD_FILE_PATH + fileName);
//8k缓存数据
byte[] buffer = new byte[1024 * 8];
//设置读进缓存的字节数
int len;
while((len=is.read(buffer))!=-1){
//将缓存数据写入磁盘
os.write(buffer,0,len);
}
//关闭输出流
os.close();
//关闭输入流
is.close();
}
}
-------------------------------------------------------------------------------------
关于由jsp到action的参数传递过程如上面有色标记。
在action的测试语句中得出值为null.
如果要在jsp中将“上传文件路径”即保存在服务器中的路径,作为参数传递给action该怎么做呢?这样做不可以么?
但是试图在jsp中通过request.setAttribute()方法,将存储路径作为参数传递给action,但是在action中使用request.getAttribute()得到的值为null.这是为什么呢?不能这样做么?
下面是我的代码:
1。up.jsp
-------------------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传表单</title>
</head>
<body>
<html:form action="/upload" parameter="file_path" enctype="multipart/form-data">
[color=FF0000]<%
String file_path = "file_path";
request.setAttribute("file_path",file_path);
%>[/color]
上传文件:
<html:file property="uploadFile"></html:file>
<html:submit value="上传文件"></html:submit>
</html:form>
</body>
</html>
-------------------------------------------------------------------------------------
2.FileUploadForm.java
-------------------------------------------------------------------------------------
package com.upload.struts;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
/**
* 基于Struts FormFile的文件上传.
*/
public class FileUploadForm extends ActionForm{
/**
* 上传的文件
*/
private FormFile uploadFile;
public FormFile getUploadFile() {
return uploadFile;
}
public void setUploadFile(FormFile uploadFile) {
this.uploadFile = uploadFile;
}
}
-------------------------------------------------------------------------------------
3.FileUploadAction.java
-------------------------------------------------------------------------------------
package com.upload.struts;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
/**
* 文件上传的Action,该类用于处理FormFile及将上传的文件保存到磁盘的指定位置.
*/
public class FileUploadAction extends Action {
/**
* 上传的文件保存在服务器的路径
*/
private String UPLOAD_FILE_PATH;
private String uploadfilePath;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
/* System.out.println("----------"+(String)request.getAttribute("file_path")+ "----------");
System.out.println("----------"+ request.getAttribute("file_path") + "----------");*/
[color=FF00FF]this.UPLOAD_FILE_PATH = request.getRealPath("/")+request.getAttribute("file_path") +"\\";[/color]
File targetFolder = new File(UPLOAD_FILE_PATH);
if (targetFolder.isFile() || !targetFolder.exists()) {
targetFolder.mkdir();
}
FileUploadForm uploadForm = (FileUploadForm)form;
//得到上传的文件
FormFile uploadFile = uploadForm.getUploadFile();
//得到文件名
String fileName = uploadFile.getFileName();
//得到文件大小
int fileSize = uploadFile.getFileSize();
//文件重命名保证唯一性,通过在文件名前加入时间前缀来实现。
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
java.util.Date mydate=new java.util.Date();
fileName = sdf.format(mydate) + "_" + fileName;
uploadfilePath=this.UPLOAD_FILE_PATH + fileName;
boolean result = true;
//文件最大限额
int fileMaxSize = 100 * 1024 * 1024;
if (fileSize > fileMaxSize) {
System.out.println("文件超过规定大小!!");
result = false;
}else{
try{
//得到文件的输入流
InputStream is = uploadFile.getInputStream();
//上传文件
uploadFile(fileName,is);
System.out.println("FileName = " + fileName);
System.out.println("FileSize= " + fileSize);
System.out.println("FilePath= " + uploadfilePath);
}catch(IOException ex){
ex.printStackTrace();
//假如上传文件失败,设置一个失败的标记位
result = false;
}
}
//页面跳转。
if(result){
request.setAttribute("size", fileSize);
request.setAttribute("fileName", fileName);
return mapping.findForward("success");
} else {
return mapping.findForward("fail");
}
}
/**
* 上传文件
* @param fileName
* @param is
* @throws IOException
*/
private void uploadFile(String fileName,InputStream is) throws IOException{
OutputStream os = new FileOutputStream(UPLOAD_FILE_PATH + fileName);
//8k缓存数据
byte[] buffer = new byte[1024 * 8];
//设置读进缓存的字节数
int len;
while((len=is.read(buffer))!=-1){
//将缓存数据写入磁盘
os.write(buffer,0,len);
}
//关闭输出流
os.close();
//关闭输入流
is.close();
}
}
-------------------------------------------------------------------------------------
关于由jsp到action的参数传递过程如上面有色标记。
在action的测试语句中得出值为null.
如果要在jsp中将“上传文件路径”即保存在服务器中的路径,作为参数传递给action该怎么做呢?这样做不可以么?