页面代码如下
<form action="../UploadImageServlet" id="form1" name="form1" encType="multipart/form-data" method="post" target="hidden1_frame" onSubmit="return ckeckThumb()">
<iframe name='hidden1_frame' id="hidden1_frame" style='display:none'></iframe>
<input class="button5" name="btn_Image" type="submit" value="" >
<div style="display:none">
<input name="bigImage" type="hidden" id="bigImage" value="../images/setting/D.JPG" />
left:
<input name="left" id="left" type="text" value=""/>
<br>
top:
<input name="top" id="top" type="text" value=""/>
<br>
img_x:
<input name="img_x" id="img_x" type="text" value=""/>
<br>
img_y:
<input name="img_y" id="img_y" type="text" value=""/>
<br>
img_w:
<input name="img_w" id="img_w" type="text" value=""/>
<br>
img_h:
<input name="img_h" id="img_h" type="text" value=""/>
<br>
dst_x:
<input name="dst_x" id="dst_x" type="text" value=""/>
<br>
dst_y:
<input name="dst_y" id="dst_y" type="text" value=""/>
<br>
dst_w:
<input name="dst_w" id="dst_w" type="text" value=""/>
<br>
dst_h:
<input name="dst_h" id="dst_h" type="text" value=""/>
<br>
倍数:
<input name="f" id="f" type="text" value=""/>
<br>
宽度:
<input name="width" id="width" type="text" value=""/>
<br>
高度:
<input name="height" id="height" type="text" value=""/>
<br>
</div>
</div>
</div>
<div class="upload" style="float:left;">
<p class="upload"><span class="l_tag">请选择照片文件:</span>
<input id="text" class="text" type="text" style="float:left;width:180px;margin-right:5px" onblur="this.className=''" onfocus="this.className='input1'">
<span class="file">
<input id="file1" type="file" name="file1" value="up" onfocus="return change();"/>
<label id="labelpic"><span style="display:none;">格式错误</span></label>
</span>
</form>servlet代码如下
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { PrintWriter out = response.getWriter();
response.setCharacterEncoding("gb18030");
response.setHeader("Charset","gb18030");
response.setHeader("Cache-Control", "no-cache");
String filepath="",msg="",ext="",trace="";
String clew = "";
SmartUpload mySmartUpload = new SmartUpload();
boolean succ=true;
int length = 0;
String dst_x = request.getParameter("dst_x");
System.out.println(dst_x);
String dst_y = request.getParameter("dst_y");
System.out.println(dst_y);
String dst_w = request.getParameter("dst_w");
System.out.println(dst_w);
String dst_h = request.getParameter("dst_h");
System.out.println(dst_h);
try{
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaa");
mySmartUpload.initialize(getServletConfig(),request,response);
mySmartUpload.setMaxFileSize(2*1024*1024);
mySmartUpload.setTotalMaxFileSize(2*1024*1024);
String allowed = "jpg,bmp,gif,jpeg,png";
String denied = "exe,bat,bin,jar,html,js,htm";
mySmartUpload.setAllowedFilesList(allowed);
mySmartUpload.setDeniedFilesList(denied);
mySmartUpload.upload();
length = mySmartUpload.getFiles().getCount();
System.out.println(mySmartUpload.getFiles().getCount());
for (int i=0;i<length;i++)
{
//System.out.println("进入for循环");
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//System.out.println("进入if体");
ext = myFile.getFileExt().toLowerCase();//格式
System.out.println(ext);
//全名(绝对路径+名称)
String myFilePath = myFile.getFilePathName();//文件来源全名
//取得文件的大小
int fileSize = myFile.getSize();
System.out.println(fileSize);
//另存路径
//String jspPath = application.getRealPath(request.getRequestURI());//本JSP文件的服务器绝对路径
String filePath = getServletContext().getRealPath("/upload/Gravatar/");//本WEB程序根目录服务器绝对路径
//System.out.println(filePath);
//filePath += "\\UserFiles\\Image\\"; //要创建的文件最终保存目录
java.io.File file = new java.io.File( filePath );
// System.out.println("filepath==="+filePath);
if(!file.exists())
file.mkdirs();
trace += ((new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()))+"."+ext);//最终保存路径
//request.setAttribute("Gravatar",trace);
//System.out.println("trace==="+trace);
myFile.saveAs(filePath + "\\" + trace);
// 将文件另存
//myFile.saveAs("\\upload\\" + myFile.getFileName());
// 另存到以WEB应用程序的根目录为文件根目录的目录下
//myFile.saveAs("\\upload\\" + myFile.getFileName(), SmartUpload.SAVE_VIRTUAL);
//将文件另存到指定目录(操作系统的目录)
//myFile.saveAs(trace, SmartUpload.SAVE_PHYSICAL);
}
else
{
// 若文件不存在则继续
continue;
}
}
}catch(SmartUploadException sue){
succ=false;
clew = "上传失败";
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
}catch(Exception ex){
succ=false;
clew = "上传失败";
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
}
if(length==0){
clew = "上传失败";
succ = false;
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
}
if(succ){
clew = "文件上传成功";
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
//System.out.println("上传成功");
//request.getSession().setAttribute("Gravatar",trace);
}else
System.out.println("上传失败!!!!!!!!!!!");
out.close();
}
可以获取type=file 上传的文件,但是获取不到type=text 的值 在页面做alert时 可以获得到值 为什么file和text一起提交的时候获取不到text的值呢 还是这样的提交只能获取到file的值
<form action="../UploadImageServlet" id="form1" name="form1" encType="multipart/form-data" method="post" target="hidden1_frame" onSubmit="return ckeckThumb()">
<iframe name='hidden1_frame' id="hidden1_frame" style='display:none'></iframe>
<input class="button5" name="btn_Image" type="submit" value="" >
<div style="display:none">
<input name="bigImage" type="hidden" id="bigImage" value="../images/setting/D.JPG" />
left:
<input name="left" id="left" type="text" value=""/>
<br>
top:
<input name="top" id="top" type="text" value=""/>
<br>
img_x:
<input name="img_x" id="img_x" type="text" value=""/>
<br>
img_y:
<input name="img_y" id="img_y" type="text" value=""/>
<br>
img_w:
<input name="img_w" id="img_w" type="text" value=""/>
<br>
img_h:
<input name="img_h" id="img_h" type="text" value=""/>
<br>
dst_x:
<input name="dst_x" id="dst_x" type="text" value=""/>
<br>
dst_y:
<input name="dst_y" id="dst_y" type="text" value=""/>
<br>
dst_w:
<input name="dst_w" id="dst_w" type="text" value=""/>
<br>
dst_h:
<input name="dst_h" id="dst_h" type="text" value=""/>
<br>
倍数:
<input name="f" id="f" type="text" value=""/>
<br>
宽度:
<input name="width" id="width" type="text" value=""/>
<br>
高度:
<input name="height" id="height" type="text" value=""/>
<br>
</div>
</div>
</div>
<div class="upload" style="float:left;">
<p class="upload"><span class="l_tag">请选择照片文件:</span>
<input id="text" class="text" type="text" style="float:left;width:180px;margin-right:5px" onblur="this.className=''" onfocus="this.className='input1'">
<span class="file">
<input id="file1" type="file" name="file1" value="up" onfocus="return change();"/>
<label id="labelpic"><span style="display:none;">格式错误</span></label>
</span>
</form>servlet代码如下
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { PrintWriter out = response.getWriter();
response.setCharacterEncoding("gb18030");
response.setHeader("Charset","gb18030");
response.setHeader("Cache-Control", "no-cache");
String filepath="",msg="",ext="",trace="";
String clew = "";
SmartUpload mySmartUpload = new SmartUpload();
boolean succ=true;
int length = 0;
String dst_x = request.getParameter("dst_x");
System.out.println(dst_x);
String dst_y = request.getParameter("dst_y");
System.out.println(dst_y);
String dst_w = request.getParameter("dst_w");
System.out.println(dst_w);
String dst_h = request.getParameter("dst_h");
System.out.println(dst_h);
try{
System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaa");
mySmartUpload.initialize(getServletConfig(),request,response);
mySmartUpload.setMaxFileSize(2*1024*1024);
mySmartUpload.setTotalMaxFileSize(2*1024*1024);
String allowed = "jpg,bmp,gif,jpeg,png";
String denied = "exe,bat,bin,jar,html,js,htm";
mySmartUpload.setAllowedFilesList(allowed);
mySmartUpload.setDeniedFilesList(denied);
mySmartUpload.upload();
length = mySmartUpload.getFiles().getCount();
System.out.println(mySmartUpload.getFiles().getCount());
for (int i=0;i<length;i++)
{
//System.out.println("进入for循环");
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//System.out.println("进入if体");
ext = myFile.getFileExt().toLowerCase();//格式
System.out.println(ext);
//全名(绝对路径+名称)
String myFilePath = myFile.getFilePathName();//文件来源全名
//取得文件的大小
int fileSize = myFile.getSize();
System.out.println(fileSize);
//另存路径
//String jspPath = application.getRealPath(request.getRequestURI());//本JSP文件的服务器绝对路径
String filePath = getServletContext().getRealPath("/upload/Gravatar/");//本WEB程序根目录服务器绝对路径
//System.out.println(filePath);
//filePath += "\\UserFiles\\Image\\"; //要创建的文件最终保存目录
java.io.File file = new java.io.File( filePath );
// System.out.println("filepath==="+filePath);
if(!file.exists())
file.mkdirs();
trace += ((new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()))+"."+ext);//最终保存路径
//request.setAttribute("Gravatar",trace);
//System.out.println("trace==="+trace);
myFile.saveAs(filePath + "\\" + trace);
// 将文件另存
//myFile.saveAs("\\upload\\" + myFile.getFileName());
// 另存到以WEB应用程序的根目录为文件根目录的目录下
//myFile.saveAs("\\upload\\" + myFile.getFileName(), SmartUpload.SAVE_VIRTUAL);
//将文件另存到指定目录(操作系统的目录)
//myFile.saveAs(trace, SmartUpload.SAVE_PHYSICAL);
}
else
{
// 若文件不存在则继续
continue;
}
}
}catch(SmartUploadException sue){
succ=false;
clew = "上传失败";
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
}catch(Exception ex){
succ=false;
clew = "上传失败";
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
}
if(length==0){
clew = "上传失败";
succ = false;
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
}
if(succ){
clew = "文件上传成功";
out.print("<script>alert('"+new String(clew.getBytes("gb18030"),"iso-8859-1")+"');window.close();</script>");
//System.out.println("上传成功");
//request.getSession().setAttribute("Gravatar",trace);
}else
System.out.println("上传失败!!!!!!!!!!!");
out.close();
}
可以获取type=file 上传的文件,但是获取不到type=text 的值 在页面做alert时 可以获得到值 为什么file和text一起提交的时候获取不到text的值呢 还是这样的提交只能获取到file的值
解决方案 »
- 以前学习C#的,现在学习JAVA,有木有好的学习资料呢?如何能快速上手?
- 如何设置textarea 部分文字不可编辑??
- 不知道是什么错误,闲着的前辈们可以进来看下。
- [help]html写文件的问题
- Java程序连接mysql时出错是为什么?
- java的学习流程
- tomcat的 BufferOverflowException 如何解决
- java怎样访问delphi的数据库文件*.db?
- 怎么通过session的ID号,判断这个session是否已经失效!散分了!!在线等待!!
- jsp登陆验证模块出现异常
- 如何使用JfreeChart 绘制出一个点的坐标,比如(2,3)
- smartUpload上传文件
text和file这两个是不一样的,你可以换成两个form分别传,也可以参考apache上传包的解决方法。或直接用他的包上传
你可以用上传类中的Request 对象来获取表单数值即SmartUpload mySmartUpload = new SmartUpload();
Request req=mySmartUpload.getRequest()。你是一下肯定正确的。
是我自己写的问题
mySmartUpload.upload();
位置放在获取文件之后了,所以才没有获取到的。谢谢了