@Override
    public String execute() throws Exception {
        InputStream is = new FileInputStream(getFile());
        String root = ServletActionContext.getRequest().getRealPath("/upload");
        System.out.println("========="+root);
        
        File destFile = new File(root, this.getFileFileName());
        OutputStream os = new FileOutputStream(destFile);
        byte[] buffer = new byte[50];
        int length = 0;
        while ((length - is.read(buffer)) > 0){
            os.write(buffer, 0, length);
        }
        is.close();
        os.close();
        return "result";
}我每次上传的文件或者图片字节都是0,打不开,那个大哥大姐帮我看一下。。纠正一下错误,或者给一段可以用的上传代码给我。。谢谢了……………………在线等哦!!!

解决方案 »

  1.   

    while ((length - is.read(buffer)) > 0){  改为
    while ((length = is.read(buffer)) > 0){
      

  2.   

    查看你的JSP页面那个form的data-type是不是上传文件的格式
      

  3.   

    是的
    <s:form action="upload" enctype="multipart/form-data">
        <s:textfield name="username" id="username" label="username"/>
        <s:file name="file" id="file" label="file"/>
        <s:submit/>
    </s:form>
      

  4.   

    那你的后台用的是formfile接收的吗?
      

  5.   


    我后台是这样写的

        @Override
        public String execute() throws Exception {
        
            InputStream is = new FileInputStream(getFileName());
            String root = ServletActionContext.getRequest().getRealPath("/upload");
            System.out.println("========="+root);
            
            File destFile = new File(root,this.getFileName());
            OutputStream os = new FileOutputStream(destFile);
            byte[] buffer = new byte[50];
            int length = 0;
            while ((length = is.read(buffer)) > 0){
                os.write(buffer, 0, length);
            }
            is.close();
            os.close();
            return "result";
        } 现在报错java.io.FileNotFoundException: E:\hub\huborcl\WebRoot\upload\tmp\upload_3393dfb8_129b61967e2__8000_00000003.tmp (系统找不到指定的路径。)
    java.io.FileOutputStream.open(Native Method)
    java.io.FileOutputStream.<init>(FileOutputStream.java:179)
    java.io.FileOutputStream.<init>(FileOutputStream.java:131)
    cn.edu.hust.document.action.DocumentAction.execute(DocumentAction.java:89)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      

  6.   

    \tmp
    这个文件夹有没有?没有自己建一下
      

  7.   


    这个肯定是这么改的,如果是while ((length - is.read(buffer)) > 0)这样,肯定是有问题的,你现在的问题是路径不对,E:\hub\huborcl\WebRoot\upload这个路径找不到,手动创建这个目录,再试。
      

  8.   

    算了,我上传一个例子吧,这个可以同时上传多个附件。<s:form name="uploadFiles" action="uploadFiles.action" enctype="multipart/form-data">
    <s:file name="upload" label="文件:"></s:file><br />
    <s:file name="upload" label="文件:"></s:file><br />
    <s:file name="upload" label="文件:"></s:file><br />
    <s:submit value="确定"></s:submit>
    </s:form>package com.learn.web.action;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;import com.opensymphony.xwork2.Action;public class FileUploadAction implements Action { private File[] upload;
    private String[] uploadFileName;
    private String[] uploadContentType;
    private String allowFiles; public String execute() throws Exception {  System.out.println("uploadFileName: " + uploadFileName + "   uploadContentType: " + uploadContentType);

    for(int i = 0;i < upload.length;i++) { 
    FileOutputStream fos = new FileOutputStream("C:/" + uploadFileName[i]);
    FileInputStream fis = new FileInputStream(upload[i]);
    byte[] buffer = new byte[1024];
    int len = 0;
    while((len = fis.read(buffer)) > 0) 
    fos.write(buffer, 0, len);
    }
    return SUCCESS;
    } public File[] getUpload() {
    return upload;
    } public void setUpload(File[] upload) {
    this.upload = upload;
    } public String[] getUploadFileName() {
    return uploadFileName;
    } public void setUploadFileName(String[] uploadFileName) {
    this.uploadFileName = uploadFileName;
    } public String[] getUploadContentType() {
    return uploadContentType;
    } public void setUploadContentType(String[] uploadContentType) {
    this.uploadContentType = uploadContentType;
    } public String getAllowFiles() {
    return allowFiles;
    } public void setAllowFiles(String allowFiles) {
    this.allowFiles = allowFiles;
    }

    }
      

  9.   

    SmartUpload 使用这个跟简单 而且方便