用Struts upload上传文件添加描述问题

可乐渴了吗 2010-01-25 02:12:29
JSP页面代码


<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html:html>
<head>
<title>用Struts上传文件</title>
<link href="<%=request.getContextPath()%>/XlsManager/css/css.css" rel="stylesheet" media="all" type="text/css" />
</head>
<body>
<html:form action="/uploadsAction" enctype="multipart/form-data">
<table id="mytable" cellspacing="0" align=center>
<tr>
<th scope="col" colspan="2" align=center>数据文件上传</th>
</tr>
<tr>
<td class="row" width=200>文件一:</td>
<td class="row" width=300><html:file property="theFile"/></td>
</tr>
<tr>
<td class="row" width=200>所属分类</td>
<td class="row" width=300><select size="1" name="InType">
<c:forEach var="xl" items="${xl}">
<option selected value="${xl.type }">${xl.type }</option>
</c:forEach>
</select></td>
</tr>
<tr><td colspan=2 align=center><input type="submit" value="上传"></td></tr>
</table>
</html:form>
</body>
</html:html>



UpLoadAction代码

package com.XlsFile;

import java.io.*;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
import com.DBConn.struts.UpLoadForm;


public class UpLoadAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
{
response.setContentType("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
}
UpLoadForm theForm = (UpLoadForm) form;
//取得上传的文件
FormFile file = theForm.getTheFile();
FormFile file2 = theForm.getTheFile2();
FormFile file3 = theForm.getTheFile3();
FormFile file4 = theForm.getTheFile4();
FormFile file5 = theForm.getTheFile5();

System.out.println(theForm.getInType());
String filePath = this.getServlet().getServletContext()
.getRealPath("/");
Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(Calendar.YEAR) ;
int moth = rightNow.get(Calendar.MONTH)+1;
int date = rightNow.get(Calendar.DATE);

String FilePath=filePath+"UploadFiles\\"+String.valueOf(year)+"\\"+
String.valueOf(moth)+"\\"+String.valueOf(date);
newFolder(filePath+"UploadFiles\\"+String.valueOf(year));
newFolder(filePath+"UploadFiles\\"+String.valueOf(year)+"\\"+
String.valueOf(moth));
newFolder(filePath+"UploadFiles\\"+String.valueOf(year)+"\\"+
String.valueOf(moth)+"\\"+String.valueOf(date));
try
{

String FN=file.getFileName();

if(FN.length()>1)
{


InputStream stream = file.getInputStream();//把文件读入
OutputStream bos = new FileOutputStream(FilePath+"\\"+file.getFileName());
request.setAttribute("fileName",filePath + "/"
+ file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
{
bos.write(buffer, 0, bytesRead);//将文件写入服务器
}

// System.out.println(rightNow.get(Calendar.YEAR));
bos.close();
stream.close();
}




} catch (Exception e)
{
System.err.print(e);
}
return mapping.findForward("display");

}

// private void println(String filePath) {
// // TODO Auto-generated method stub
//
// }

public void newFolder(String folderPath) {
try {
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
if (!myFilePath.exists()) {
myFilePath.mkdir();
}
}
catch (Exception e) {
System.out.println("新建目录操作出错");
e.printStackTrace();
}
}



}

UpLoadForm代码

package com.DBConn.struts;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;




public class UpLoadForm extends ActionForm {

private static final long serialVersionUID = 1L;
protected FormFile theFile;


private String InType;


public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {

return null;
}


public void setInType(String InType) {
this.InType = InType;
}


public String getInType() {
return InType;
}

public FormFile getTheFile()
{
return theFile;
}

public void setTheFile(FormFile theFile)
{
this.theFile = theFile;
}

public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
}

}


为什么红色的地方打印出来的值为null


我想把所属分类选中的值传过来,
谢谢了!
...全文
104 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzzfff2008 2010-01-26
  • 打赏
  • 举报
回复
楼主的UpLoadForm中定义变量名最好首字母不要大写,一般变量名都是小写开头,而且java区分大小写,一般定义了变量名之后,会在set,get方法中自动将首字母转换为大写,并且默认元件的名字是小写,所以在得值的时候,肯定它会这样查找:是去掉set,并把第一个字母变为小写,所以楼主定义的private String InType;这个变量就会找不到。。。
  • 打赏
  • 举报
回复
恩,解决了就好。
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 crazylaa 的回复:]
引用 3 楼 wufei006007008 的回复:
解决就好 把分分了吧
哈哈jf
[/Quote]
j
longtenggdf 2010-01-26
  • 打赏
  • 举报
回复
上传文件的form里面不能包含其他标签,参数会取不到。我的做法在提交之前将所要传递的参数封装到URL里面。
tuo_bing 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wufei006007008 的回复:]
解决就好 把分分了吧

[/Quote]

up
crazylaa 2010-01-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wufei006007008 的回复:]
解决就好 把分分了吧
[/Quote]哈哈jf
wufei006007008 2010-01-25
  • 打赏
  • 举报
回复
解决就好 把分分了吧
可乐渴了吗 2010-01-25
  • 打赏
  • 举报
回复
String intype=request.getParameter("InType");
就可以获取了!
可乐渴了吗 2010-01-25
  • 打赏
  • 举报
回复
救命呀

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧