日期:2014-05-17  浏览次数:20646 次

文件上传
index.jsp

 <form action="<%=request.getContextPath() %>/wuda.do" method= "post" enctype= "multipart/form-data" 
  >
  用户名:<input type="text" name="name"/>
  头像:<input TYPE="FILE" name="pic"/>
<input type="submit" value="提交"/>
</form>
   


form:

/**

*/
private static final long serialVersionUID = 7449749228259124331L;
public String name;
public File pic;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
  
action :


public class picdemo extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

pickdemoform pic=(pickdemoform)form;

File b=pic.getPic();

String name=pic.getName();

String ReturnKeyWord="fa";

System.out.println(name);
System.out.println(b);

if(picapp.saveFile(b, name, new SqlServer())){

ReturnKeyWord="su";
}



return mapping.findForward(ReturnKeyWord);
}

}

总显示找不到文件路径,在本机上有时候能够上传能够获取完整路径,但有时候就不能获取完整路径不能上传,在别人登录我的网页同样不能上传,只能获取文件名啊??
怎么回事?是不是:<input TYPE="FILE" name="pic"/>只能获取文件名,而不能获取文件?

------解决方案--------------------

pic取的是整体文件路径。
f(upload==null||!upload.exists()){
message="对不起,该文件不存在或已被移除,请重新选择!";
return "add";
}else{
//begin upload("保存文件路径")
String str=this.getRequest().getRealPath(savePath)+"/"+resourceid;
File dest=new File(str);
if(!dest.exists())dest.mkdir();
service.copyFile(upload,new File(dest+"/"+uploadFileName));
//over
}
------解决方案--------------------
File b=pic.getPic();这句肯定有问题,pic.getPic()应该只能拿到文件路径。。
------解决方案--------------------
FORM 里面的属性是一个FormFile,
private FormFile formFile;
<input TYPE="FILE" name="formFile"/> 这里对应

然后action里, FormFile formFile = pickdemoform.getFormFile();
然后文件名什么的由 formFile 得到

------解决方案--------------------
同意楼上,