用jQuery实现文件上传

用jQuery实现文件上传
       今天本来是可以做很多东西的,但就是因为做文件上传花了我很多时间,到最后也还不知道是什么错,只是模糊的‘感觉’到错误在action里面,现在我把错误重现一下,以便以后能更快的解决这方面的错误:
首先看一下我的jsp页面的代码:
<script type="text/javascript">
$(document).ready(function() {
  $('#fileInputByUploadify').fileUpload({
    'uploader':'image/uploadify/uploader.swf',
    'script':'save.action',
    'cancelImg':'image/uploadify/cancel.png',
    'fileDataName':'upload',
    'onComplete':function(){
    alert('上传成功');
   }
  });
});
</script>

<input type="file" id="fileInputByUploadify"/><br />
<a href="javascript:$('#fileInputByUploadify').fileUploadStart();">上传文件</a>
要实现的功能是点击上传文件时触发上面的save这个action;
struts配置文件:<struts>
<constant name="struts.objectFactory"
  value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<package name="/getstart" extends="json-default">

  <action name="uploadFile" class="uploadAction" method="save">
   <result name="success">
    upload_success.jsp
   </result>
  </action>
 
</package>
</struts>

java代码:
public class UploadAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private Pictures pictures;
public File upload;
private String uploadContentType;
private String uploadFileName;
private InputStream inputStream;
private PicturesDao picturesDao;
public PicturesDao getPicturesDao() {
  return picturesDao;
}
public void setPicturesDao(PicturesDao picturesDao) {
  this.picturesDao = picturesDao;
}
public String getUploadContentType() {
  return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
  return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
}
public InputStream getInputStream() {
  return inputStream;
}
public void setInputStream(InputStream inputStream) {
  this.inputStream = inputStream;
}
public File getUpload() {
  return upload;
}
public void setUpload(File upload) {
  this.upload = upload;
}
public Pictures getPictures() {
  return pictures;
}
public void setPictures(Pictures pictures) {
  this.pictures = pictures;
}
@Transactional(propagation=Propagation.REQUIRED)
public String save(){
  System.out.println("<<<<<<<<<<<<<<<<<<<<");
  if(this.upload != null){
   System.out.println(this.upload.getName());
   this.pictures = new Pictures();
   try {
    this.inputStream = new FileInputStream(this.getUpload());
    this.pictures.setData(new byte[(int)this.getUpload().length()]);
    inputStream.read(this.pictures.getData());
    inputStream.close();
   } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
   }
   this.picturesDao.create(pictures);
  }
  return SUCCESS;
}
}
《注:因为使用的是struts+hibernate+spring的脚手架,里面的dao是使用sping在配置文件里面做注入的》
它给我报的错如下:

09-4-1 16:12:59 ERROR (org.apache.struts2.dispatcher.Dispatcher:512) - Could not
find action or result
No result defined for action file.UploadAction$$EnhancerByCGLIB$$26bddd8c and re
sult input - action - file:/E:/new/workspace/proto.0.1/target/classes/file/strut
s.xml:12:64
始终进不了我的action里面,我找了很长时间;都没有发现什么地方有错,后来我把upload改成uploadFile就可以了   也不知道是不时因为这个upload是个关键字海市什么的,也就迷迷糊糊的解决了  ,起初想到去该代码的时候是因为自己先把action里面的东西删了,就留下picturesDao和upload,后来它提示错误信息是说没有uploadContextType(),folder,FileName,这些东西都没有,后来我加上了,也就不报这错,但是我加上了之后没有那些错了,知道把名字也改了就行了;


转载d_jeans的共享空间 的日志 — Windows Live

你可能感兴趣的:(spring,jquery,Hibernate,jsp,struts)