比如form1.submit();form2.submit();
之所以问这个问题是因为我现在有个困惑,我在做一个文件上传的页面,
form如下
<form name="uploadForm" method="post" action="uploadfile.jsp" enctype="multipart/form-data">
但是这样其他input里面的值就传不到后台了,所以我想用两个form,一个
上传文件,一个传值,不知道可行不
之所以问这个问题是因为我现在有个困惑,我在做一个文件上传的页面,
form如下
<form name="uploadForm" method="post" action="uploadfile.jsp" enctype="multipart/form-data">
但是这样其他input里面的值就传不到后台了,所以我想用两个form,一个
上传文件,一个传值,不知道可行不
解决方案 »
- 急!急!急!jstl 标签在我建的jee5的项目里还是要导入包到lib里?!!!
- csdn的上传文件时的进度条及上传速度什么的是怎么弄出来的呀,怎么获得这些信息值
- javascript 问题【如何学习Javascript】
- tomcat启动问题
- 有谁在用tomcat6.0啊?虚拟目录怎么配啊?
- 有用JFreeChart实现的折线图的实例吗?
- 如何用jxl设置Excel报表的表头(关于XXX的报表) 和表注(页码)
- JSP中的package问题-------------------------结帖超快!!!!!!!!!!!
- 继续问:JSP连接SQL server 2000怎么不支持滚动游标?
- 救救孩子吧,账号昵称判断不好使啊为什么,密码哪里就好用哭死
- 请问下高手,怎么才能隐藏url重写后面的参数?
- 请教高手帮忙解决JBOSS后台,总报这样的错误。。。
说的对.
file的IO流,不加只能传text和hidden的值,我现在要它们都能一次传到后台。
真是郁闷死了
function subForms(){
form1.submit();
form2.submit();
}
这样不是两个一起提交?
在jump.jsp<%@ page contentType="text/html;charset=GBK" %>
<%@ page language="java" import="com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /><HTML>
<BODY BGCOLOR="white">
<%
//初始化文件名和上传结果
String FileName = "noImage";
String result ="error";
try
{
// 执行初始化操作
mySmartUpload.initialize(pageContext);
//限制文件上传格式
mySmartUpload.setAllowedFilesList("jpg,gif,bmp,jpeg");
// 上传文件到服务器
mySmartUpload.upload(); //如果有文件上传
if(mySmartUpload.getFiles().getCount()>0)
{
// 取出文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
//
//如果文件存在
if(myFile.getSize()>0)
{
String k = myFile.getFileExt();
java.util.Date d = new java.util.Date();
FileName = d.getTime()+ k;
myFile.saveAs("/upload/" +FileName);
result="success";
}
else
{
System.out.println("文件不存在!");
//result="error";
}
}
else
{
System.out.println("没上传!");
//result="error";
}
}
catch(Exception ex)
{
System.out.println(ex);
//result="error";
}
finally
{
//存储表单信息
String Uid = mySmartUpload.getRequest().getParameter("Uid");
String Atitle = mySmartUpload.getRequest().getParameter("Atitle");
String Acontent = mySmartUpload.getRequest().getParameter("Acontent");
request.setAttribute("Uid",Uid);
request.setAttribute("Atitle",Atitle);
request.setAttribute("Acontent",Acontent);
request.setAttribute("FileName",FileName);
request.setAttribute("Result",result);
}
%>
<jsp:forward page="servlet1"/>
</BODY>
</HTML>
下载地址:http://jakarta.apache.org/commons/fileupload/
用一个FORM提交,其他的input都给上不同的名字。
例如:<INPUT type="text" name="MASTER_REVDATE" maxlength="10" size="20">
在服务端这么来取:
String _revDate = ((ParamPart)paramMap.get("MASTER_REVDATE")).getValue();
另外写一个方法来取得上传流的各部分
protected HashMap getMultipartMap(HttpServletRequest request) throws EconException {
final int MAX_CONTENT_LENGTH = 1024 * 1000 * 10;
MultipartParser parser = null;
HashMap paramMap = new HashMap();
Part eachPart = null;
try {
parser = new MultipartParser(request, MAX_CONTENT_LENGTH);
List params = parser.getEachParts();
for(int i=0 ; i<params.size() ; i++) {
eachPart = (Part)params.get(i);
paramMap.put(eachPart.getName(), params.get(i));
}
return paramMap;
} catch (FileUploadException e) {
if(e instanceof SizeLimitExceededException) {
throw new EconException(EconException.UPLOAD_SIZE_ERROR, e);
} else {
throw new EconException(EconException.ERROR, e);
}
} catch (Exception e) {
throw new EconException(EconException.ERROR, e);
}
}
楼主的问题关键在于对enctype="multipart/form-data"的form不会处理
非file型input值吧,根据你使用的组件不同,这个有区别,
给你个用apache的文件上传包commons-fileupload-1.0.jar的代码:
(请注意//*******提示的部分的处理方式)package edu.lynu.webexam.servlet;import java.io.*;
import java.util.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
import edu.lynu.webexam.common.CommonObjects;
import org.apache.commons.fileupload.*;public class UploadServlet extends WebExamBaseServlet { private static String ABSOLUTE_UPLOAD_PATH ; //Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
ABSOLUTE_UPLOAD_PATH = CommonObjects.WEBAPP_ROOT+"uploads\\";
File new_path = new File(ABSOLUTE_UPLOAD_PATH);
if(false == new_path.exists()) new_path.mkdir();
DiskFileUpload fu = new DiskFileUpload();
// maximum size before a FileUploadException will be thrown
fu.setSizeMax(1000000);
// maximum size that will be stored in memory
fu.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
fu.setRepositoryPath(ABSOLUTE_UPLOAD_PATH);
List fileItems = fu.parseRequest(request);
System.out.println("total variable numbers="+fileItems.size());
Iterator i = fileItems.iterator();
//******* assume it is variable 'process_id"
FileItem fi1=(FileItem)i.next();
System.out.println("fi1.getFieldName()="+fi1.getFieldName());
System.out.println("fi1.getString()="+ fi1.getString() ); //correct!
//******* assume it is variable 'compart_symbol"
FileItem fi2=(FileItem)i.next();
System.out.println("fi2.getFieldName()="+fi2.getFieldName());
String compart_symbol = fi2.getString();
System.out.println("fi2.getString()="+ fi2.getString() ); //correct!
//>>>>>>> assume it is variable 'import_txt" file control
FileItem fi3 = (FileItem)i.next();
System.out.println("fi3.getFieldName()="+fi3.getFieldName());
String fileName_tmp = fi3.getName();
System.out.println("fi3.getName()="+ fi3.getName() );
String fileName_no_path = fileName_tmp.substring(fileName_tmp.lastIndexOf("\\")+1);
File fileToWrite = null;
if( fi3.getInputStream().available()>0 ){
fileToWrite = new File( ABSOLUTE_UPLOAD_PATH + fileName_no_path );
fi3.write(fileToWrite);
}
if( fileToWrite != null && true==ImportTxtFile(fileToWrite, compart_symbol) ){
request.setAttribute("file_name",(ABSOLUTE_UPLOAD_PATH + fileName_no_path).replace("\\","/"));
request.getRequestDispatcher(CommonObjects.JSP_ROOT2+"UploadOK.jsp").forward(request, response);
}else{
response.sendRedirect(CommonObjects.JSP_ROOT+"error.jsp");
}
}catch(Exception e){
System.out.println("Exception ocurred while UPLOAD"+e);
e.printStackTrace();
response.getOutputStream().println("upload fail");
return;
}
}
private boolean ImportTxtFile( File import_txt_file, String symbol ) {
return true;
}
}
<INPUT TYPE="text" NAME="ID" >
<INPUT TYPE="submit" value="对 比" onclick="document.info.action='Compare.asp'; return document.returnValue;">
<INPUT TYPE="submit" value="删 除" onclick="document.info.action='index.asp?Action=Del'; return document.returnValue;">
<INPUT TYPE="submit" value="修 改" onclick="document.info.action='index.asp?Action=Edit'; return document.returnValue;">
<INPUT TYPE="submit" value="刷 新" onclick="document.info.action='index.asp?Action=ReOrder'; return document.returnValue;">
<INPUT TYPE="submit" value="广 告" onclick="document.info.action='index.asp?Action=Ad'; return document.returnValue;">
<INPUT TYPE="submit" value="已 售" onclick="document.info.action='index.asp?Action=Sale'; return document.returnValue;">
</form>
这样就可以了
我是多个form提交到一个地方,老大
document.forms[1].submit();
document.forms[2].submit();
.
.
.
.
document.forms[N].submit();
SmartUpload su = new SmartUpload();
// 上传初始化
su.initialize(pageContext);
//不要用request.getParameter(name);//用SmartUpload 提供的方法等到request就可以了
su.getRequest().getParameter( "Uid ");