我想上传照片,点击上传,弹出一个框选择照片,由form表单提交到servlet中,可是servlet中得不到弹出框form表单传过来的内容
<div id="edit">
<form action="../StaffServlet" method="post" enctype="multipart/form-data">
<input type="hidden" name="status" value="InsertPhoto" />
<input type="hidden" name="id" value='<%=staf.getId() %>' />
<span style="font-size:14px;">上传照片</span>
<div>
<lable>照片地址: </lable>
<input type="file" name="pic"/>
</div>
<div> </div>
<div>
<lable>照片要求: </lable>
<font color="red">格式为jpg,png,gif</font>
</div>
<div> </div>
<div>
<input type="submit" name="insert" value="上传照片"/>
<span> </span>
<input type="button" value="关闭" onclick="javascript:wclose('edit');" />
</div>
</form>
</div>
我用的是Smartupload组件,servlet那边: SmartUpload smart = new SmartUpload();
// 接收要操作的参数值
String status = null ; if(request.getParameter("status") != null)
{
status = request.getParameter("status");
System.out.println("1" + status);
} if(status == null)
{
//上传初始化
smart.initialize(this.getServletConfig(), request, response);
//准备上传
try
{
//准备上传
smart.upload();
} catch (SmartUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
status = smart.getRequest().getParameter("status");
System.out.println("2" + status);
}
<div id="edit">
<form action="../StaffServlet" method="post" enctype="multipart/form-data">
<input type="hidden" name="status" value="InsertPhoto" />
<input type="hidden" name="id" value='<%=staf.getId() %>' />
<span style="font-size:14px;">上传照片</span>
<div>
<lable>照片地址: </lable>
<input type="file" name="pic"/>
</div>
<div> </div>
<div>
<lable>照片要求: </lable>
<font color="red">格式为jpg,png,gif</font>
</div>
<div> </div>
<div>
<input type="submit" name="insert" value="上传照片"/>
<span> </span>
<input type="button" value="关闭" onclick="javascript:wclose('edit');" />
</div>
</form>
</div>
我用的是Smartupload组件,servlet那边: SmartUpload smart = new SmartUpload();
// 接收要操作的参数值
String status = null ; if(request.getParameter("status") != null)
{
status = request.getParameter("status");
System.out.println("1" + status);
} if(status == null)
{
//上传初始化
smart.initialize(this.getServletConfig(), request, response);
//准备上传
try
{
//准备上传
smart.upload();
} catch (SmartUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
status = smart.getRequest().getParameter("status");
System.out.println("2" + status);
}
解决方案 »
- 求利用短信猫发短信的接口,写了一个好像不怎么对?
- ide问题
- sso单点登录解决方案
- Java Persistence with Hibernate中文版Hibernate实战第2版出版
- 在启动TOMCAT时出现java.lang.ClassCastException: org.apache.naming.resources.FileDirContext错误
- 求《基于Struts的企业人事信息管理系统的设计与实现》资料
- ejb发生异常,请进来看看,多谢了
- indexof(int int);
- j2sdk和j2sdkee有什么不同呀?分别在什么地方应用?
- 这个equals()方法怎么重写呢?
- 关于 hibernate 事务
- 关于hibernate 多对多中间表主键的问题
<div id="edit">
<form action="../StaffServlet" method="post" enctype="multipart/form-data">
<input type="hidden" name="status" value="InsertPhoto" />
<input type="hidden" name="id" value='<%=staf.getId() %>' />
<span style="font-size:14px;">上传照片</span>
<div>
<lable>照片地址: </lable>
<input type="file" name="pic"/>
</div>
<div> </div>
<div>
<lable>照片要求: </lable>
<font color="red">格式为jpg,png,gif</font>
</div>
<div> </div>
<div>
<input type="submit" name="insert" value="上传照片"/>
<span> </span>
<input type="button" value="关闭" onclick="javascript:wclose('edit');" />
</div>
</form>
</div>
我用的是Smartupload组件,servlet那边: SmartUpload smart = new SmartUpload();// 接收要操作的参数值
String status = null ;if(request.getParameter("status") != null)
{
status = request.getParameter("status");
System.out.println("1" + status);
}if(status == null)
{//上传初始化
smart.initialize(this.getServletConfig(), request, response);//准备上传
try
{//准备上传
smart.upload();} catch (SmartUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}status = smart.getRequest().getParameter("status");
System.out.println("2" + status);
}
好的,我调整一下格式吧
这么写可以那得到啊!
你把request.getParameter("status")也换成
smart.getRequest().getParameter("status");试试!
在servlet里面 你的form名称 daf = (你的form名称)form;
FormFile imgFile=daf.getImgFile();
别的如上;
如果是struts2,在action里面直接定义即可
InputStream is = (InputStream)request.getInputStream();byte[] buf = new byte[1024];
int readn;while((readn = is.read(buf))!=-1){
System.out.println(new String(buf,0,readn));
}is.close();打印下你就明白了,不过还是推荐你用框架。common-upload.jar。
package com.ybjz.servlet;import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Date;import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;import com.ybjz.factory.*;
import com.ybjz.util.IPTimeStamp;
import com.ybjz.vo.*;
import org.lxh.smart.*;public class StaffServlet extends HttpServlet
{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.setCharacterEncoding("gb2312") ;
String path = "errors.jsp" ;
SmartUpload smart = new SmartUpload();
// 接收要操作的参数值
String status = null ; if(request.getParameter("status") != null)
{
status = request.getParameter("status");
System.out.println("1" + status);
} if(status == null)
{
//上传初始化
smart.initialize(this.getServletConfig(), request, response);
//准备上传
try
{
//准备上传
smart.upload();
} catch (SmartUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
status = smart.getRequest().getParameter("status");
System.out.println("2" + status);
} if(status != null)
{
//恢复或操作
if(status.equals("return")||status.equals("drop"))
{
String id = request.getParameter("id"); try
{
DAOFactory.getStaffDAOInstance().recycle(id, status);
}catch(Exception e)
{
e.printStackTrace();
}
if(status.equals("return"))
{
path = "edit/yghsz.jsp";//恢复跳向
}
if(status.equals("drop"))
{
path = "edit/glyg.jsp";//删除跳向
}
}
//彻底删除
if(status.equals("delete"))
{
String id = request.getParameter("id");
try
{
DAOFactory.getStaffDAOInstance().delete(id);
}catch(Exception e)
{
e.printStackTrace();
}
path = "edit/yghsz.jsp";//彻底删除跳向
}
//插入操作
if(status.equals("insert"))
{
//获得参数
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String nation = request.getParameter("nation");
String edu = request.getParameter("edu");
String style = request.getParameter("style");
String home = request.getParameter("home");
String time = request.getParameter("time");
String height = request.getParameter("height");
String weight = request.getParameter("weight");
String health = request.getParameter("health");
String requires = request.getParameter("requires");
String idcard = request.getParameter("idcard");
String tel = request.getParameter("tel");
String note = request.getParameter("note");
String family = request.getParameter("family");
String work = request.getParameter("work");
String certificate = request.getParameter("certificate");
String addr = request.getParameter("addr");
String state = request.getParameter("state");
if(!name.equals("") && !sex.equals("") && !age.equals("") && !health.equals("") && !tel.equals(""))
{
Staff staf = new Staff();
staf.setName(name);
staf.setSex(sex);
staf.setAge(age);
staf.setNation(nation);
staf.setEdu(edu);
staf.setStyle(style);
staf.setHome(home);
staf.setNowtime(time);
staf.setHeight(height);
staf.setWeight(weight);
staf.setHealth(health);
staf.setRequires(requires);
staf.setIdcard(idcard);
staf.setTel(tel);
staf.setNote(note);
staf.setFamily(family);
staf.setWork(work);
staf.setCertificate(certificate);
staf.setAddr(addr);
staf.setState(state);
//存入参数
try
{
DAOFactory.getStaffDAOInstance().insert(staf);
}catch(Exception e)
{
e.printStackTrace();
}
path = "edit/temp.jsp?status=tjygSuccess";//添加员工成功
}else
{
path = "edit/temp.jsp?status=tjygLose";//添加员工失败
}
}
//修改操作
if(status.equals("update"))
{
//获得参数
String id = request.getParameter("id");
int i = Integer.parseInt(id);
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String nation = request.getParameter("nation");
String edu = request.getParameter("edu");
String style = request.getParameter("style");
String home = request.getParameter("home");
String time = request.getParameter("time");
String height = request.getParameter("height");
String weight = request.getParameter("weight");
String health = request.getParameter("health");
String requires = request.getParameter("requires");
String idcard = request.getParameter("idcard");
String tel = request.getParameter("tel");
String note = request.getParameter("note");
String family = request.getParameter("family");
String work = request.getParameter("work");
String certificate = request.getParameter("certificate");
String addr = request.getParameter("addr");
if(!name.equals("") && !sex.equals("") && !age.equals("") && !health.equals("") && !tel.equals(""))
{
Staff staf = new Staff();
staf.setId(i);
staf.setName(name);
staf.setSex(sex);
staf.setAge(age);
staf.setNation(nation);
staf.setEdu(edu);
staf.setStyle(style);
staf.setHome(home);
staf.setNowtime(time);
staf.setHeight(height);
staf.setWeight(weight);
staf.setHealth(health);
staf.setRequires(requires);
staf.setIdcard(idcard);
staf.setTel(tel);
staf.setNote(note);
staf.setFamily(family);
staf.setWork(work);
staf.setCertificate(certificate);
staf.setAddr(addr);
//存入参数
try
{
DAOFactory.getStaffDAOInstance().update(staf);
}catch(Exception e)
{
e.printStackTrace();
}
path = "edit/temp.jsp?status=ygUpdateSuccess";//修改员工成功
}else
{
path = "edit/temp.jsp?status=ygUpdateLose";//修改员工失败
}
}
//插入照片操作
if(status.equals("InsertPhoto"))
{
String id = smart.getRequest().getParameter("id");
//获得上传文件扩展名
String ext = smart.getFiles().getFile(0).getFileExt();
//除三种文件外,其余都不可上传
if(ext.equals("jpg")||ext.equals("jpeg")||ext.equals("png")||ext.equals("gif"))
{
IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()) ;
String pname = its.getIPTimeStampRand() + "." + ext;
// 保存文件
try
{
smart.getFiles().getFile(0).saveAs("/upload/"+pname) ;//文件名以当前时间日期命名
try
{
DAOFactory.getStaffDAOInstance().uploadPhoto(id, pname);
}catch(Exception e)
{
e.printStackTrace();
}
} catch (Exception e1)
{
e1.printStackTrace();
}
path = "edit/temp.jsp?status=phUpdateSuccess&id="+id+"";
}else
{
System.out.println("上传图片类型错误!");
path = "edit/temp.jsp?status=phUpdateLose&id="+id+"";
}
}
}
else
{
}
response.sendRedirect(path);
//request.getRequestDispatcher(path).forward(request,response) ;
}}
我不止一个页用这个servlet,也就是说不是所有的都用enctype="multipart/form-data",所以就是用这部分if(request.getParameter("status") != null)
{
status = request.getParameter("status");
System.out.println("1" + status);
} if(status == null)
{
//上传初始化
smart.initialize(this.getServletConfig(), request, response);
//准备上传
try
{
//准备上传
smart.upload();
} catch (SmartUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
status = smart.getRequest().getParameter("status");
System.out.println("2" + status);
}
判断下哪种类型上传的。如果不用弹出的div,就是直接写一个单独的jsp页也能上传,但是把上传放到一个弹出的div中就不行了,请教,谢谢大家了