我不知道你是通过什么方法上传的,如果用jspsmart的话
是通过mySmartUpload.getRequest().getParameter("NewsTitle");
其中mySmartUpload是个bean:<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />表单用提交用<form method="post" enctype="multipart/form-data" action="upload.jsp">
是通过mySmartUpload.getRequest().getParameter("NewsTitle");
其中mySmartUpload是个bean:<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />表单用提交用<form method="post" enctype="multipart/form-data" action="upload.jsp">
下载下来的里面有例子,有帮助.
获得其他字段的方法是:
String values = myUpload.getRequest().getParameter("values");
当然 我也觉得用网上的一些组件比自己写简单多了
<%@ page language="java" import="com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<jsp:useBean id="inewBean" scope="page" class="hitachi.news.inewBean" />
<HTML>
<BODY BGCOLOR="white">
<H1>jspSmartUpload : Sample 3</H1>
<HR>
<%// Variables
int count=0;
// Initialization
mySmartUpload.initialize(pageContext);
// Only allow txt or htm files
//mySmartUpload.setAllowedFilesList("htm,html,txt,,");
// DeniedFilesList can also be used :
// mySmartUpload.setDeniedFilesList("exe,bat,jsp");
// Deny physical path
// mySmartUpload.setDenyPhysicalPath(true);
// Only allow files smaller than 50000 bytes
// mySmartUpload.setMaxFileSize(50000);
// Deny upload if the total fila size is greater than 200000 bytes
// mySmartUpload.setTotalMaxFileSize(200000);
// Upload
mySmartUpload.upload();
// Save the files with their original names in a virtual path of the web server
try {
count = mySmartUpload.save("/pic/upload/news", mySmartUpload.SAVE_VIRTUAL);
String title=mySmartUpload.getRequest().getParameter("title");
String content=mySmartUpload.getRequest().getParameter("content");
String picture=mySmartUpload.getFiles().getFile(0).getFileName();inewBean.insert_news(title,content,picture);
} catch (Exception e){
out.println("<b>Wrong selection : </b>" + e.toString());
}
// Display the number of files uploaded
out.println(count + " file(s) uploaded."); %>
</BODY>
</HTML>
<head>
</head>
<body>
<form METHOD="POST" ACTION="/hitachi/inews.jsp" ENCTYPE="multipart/form-data">
<table><tr>
<td>
标题
</td>
<td>
<input type="input" name="title"></input>
</td>
</tr><tr>
<td>
内容
</td>
<td>
<textarea name="content"></textarea>
</td>
</tr><tr>
<td>
图片
</td>
<td>
<input type="file" name="pic"></input>
</td>
</tr><tr>
<td>
</td>
<td>
<input type="submit" value="保存"></input>
</td>
</tr>
<table>
</form>
</body>
</html>
java.util.Enumeration e = myUpload.getRequest().getParameterNames(); // Retreive parameters
while (e.hasMoreElements()) { String key = (String)e.nextElement();
String[] values = myUpload.getRequest().getParameterValues(key);
// Browse the current parameter values
for(int i = 0; i < values.length; i++) {
out.print(key + " = ");
out.print(values[i] + "<BR>");
}
}
POST / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*
Accept-Language: zh-cn
Content-Type: multipart/form-data; boundary=---------------------------7d32c12b9016a
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.0)
Host: localhost:1024
Content-Length: 684
Connection: Keep-Alive
Cache-Control: no-cache-----------------------------7d32c12b9016a
Content-Disposition: form-data; name="file"; filename="D:\Documents and Settings\Administrator\My Documents\?·.txt"
Content-Type: text/plainaaaaaaaaaaaaaaaaaaaaaaaaaa
-----------------------------7d32c12b9016a
Content-Disposition: form-data; name="submit"upload
-----------------------------7d32c12b9016a--
(这里还有一个\r\n)
而且也能上传二进制文件。
从POST到第一个空白行(包括空白行),是报头的内容,也就request.getContentType()(如上:Content-Type: multipart/form-data; boundary=---------------------------7d32c12b9016a);是它里面的内容之一。剩下的内容的长度也就是:request.getContentLength()了,
我们可以通过如下代码获得Content.
DataInputStream in=new DataInputStream(request.getInputStream());
ByteArrayOutputStream byteContent=new ByteArrayOutputStream(request.getContentLength());
byte []buffer=new byte[1024];
int length;
while((length=in.read(buffer))>0)
{
byteContent.write(buffer,0,length);
}
现在传上来的文件就在byteContent中了,你注意报头中的contentType中的boundary的值和下面的那些相似的东西的关系就不难写出上传文件的JSP了,不过jsp的视图功能强一些,对于一个上传文件的程序来说,我还是赞成用servlet写的。