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
我想把所属分类选中的值传过来,
谢谢了!