1

这是我得到的错误:

java.io.FileNotFoundException: C:\Users\Owner\AppData\Roaming\NetBeans\7.3.1\config\GF3\domain1\generated\jsp\uploadRamki\data\images.jpg(系统找不到指定的路径)

这是我的支持 bean:

package beans;

import java.io.IOException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.servlet.http.Part;

@Named(value = "demoBean")
@SessionScoped
public class DemoBean implements Serializable {

    private Part file1;

    public Part getFile1() {
        return file1;
    }

    public void setFile1(Part file1) {
        this.file1 = file1;
    }

    // getters and setters for file1 and file2
    public String upload() throws IOException {
        file1.write("c:/data/" + getFilename(file1));
        return "success";
    }

    private static String getFilename(Part part) {
        for (String cd : part.getHeader("content-disposition").split(";")) {
            if (cd.trim().startsWith("filename")) {
                String filename = cd.substring(cd.indexOf('=') + 1).trim()
                    .replace("\"", "");
                return filename.substring(filename.lastIndexOf('/') + 1)
                    .substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
            }
        }
        return null;
    }
}
4

1 回答 1

0

如果您执行 part.write ,它将尝试写入运行 glassfish 的临时目录。

尝试:

        InputStream input = file1.getInputStream();
        FileOutputStream output = new FileOutputStream(URL_FILES + file1.getSubmittedFileName());
        byte[] buf = new byte[1024];
        int len;
        while ((len = input.read(buf)) > 0) {
            output.write(buf, 0, len);
        }
        input.close();
        output.close();
于 2013-10-29T01:24:12.763 回答