zxqmnzi 2008-11-08 16:41
浏览 163
已采纳

Ext结合Struts2 上传文件

每次上传的时候总是报 拒绝访问,我google了下,看到说是路径不对,字符串写错,或者权限不够,但是我看了下我的,没写错,路径正确
权限是超级管理员登陆的 怎么回事 ?还望大家给看下

[code="java"]

public String fileupload() {
    try {
        File file1 = this.getFile();
        System.out.println(file1.getName());
        String path = ServletActionContext.getServletContext().getRealPath("/upload");
        System.out.println(path);
        Util.write(path, file);
    ServletActionContext.getResponse().getWriter().println("成功");
    } catch (Exception e) {
        e.printStackTrace();
        return INPUT;
    }
    return "file";
}

[/code]

[code="java"]

//写一个静态的方法,以便调用
public static void write(String path,File file){
final int BUFFER_SIZE = 16 * 1024 ;
try {
InputStream in = null ;
OutputStream out = null ;
try {

in = new BufferedInputStream( new FileInputStream(path), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(file), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}

}

[/code]

异常的代码,就一个异常
[code="java"]
upload__683d9dab_11d7b418a9f__8000_00000000.tmp
E:\apache-tomcat-6.0.16\apache-tomcat-6.0.16\webapps\pkm\upload
java.io.FileNotFoundException: E:\apache-tomcat-6.0.16\apache-tomcat-6.0.16\webapps\pkm\upload (拒绝访问。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:137)
at java.io.FileInputStream.(FileInputStream.java:96)
at com.pkm.util.Util.write(Util.java:22)

[/code]

[b]问题补充:[/b]

那个--上一站火星。我当时改了之后但是立马在看文件夹还是只读的

回 jones 为什么我在JSP和struts1.x 里面也是这样写的OK 而struts2 就不行?

[b]问题补充:[/b]
回JSONS 我想我是明白了,但是我怎么把路径给转回来,别让我用字符串一个一个来替换,有什么好的方法没??谢谢
[b]问题补充:[/b]
回JSON
艾,咱也没找到好方法,刚才吃饭之前就是用replaceAll() 不过不知道有没什么方法,直接用上去,不替换的有米有 呵呵
[b]问题补充:[/b]
不行了,同志们,咱转换也不行
upload__14cccc4e_11d7bcd629c__8000_00000000.tmp
E:/apache-tomcat-6.0.16/apache-tomcat-6.0.16/webapps/pkm/upload
java.io.FileNotFoundException: E:\apache-tomcat-6.0.16\apache-tomcat-6.0.16\webapps\pkm\upload (拒绝访问。)

这个不行

        File file1 = this.getFile();
        System.out.println(file1.getName());
        String path = ServletActionContext.getServletContext().getRealPath("//upload");
        String root  = path.toString().replaceAll("\\\\", "/");
        System.out.println(root);
        Util.write(root, file);
    ServletActionContext.getResponse().getWriter().println("成功");

[b]问题补充:[/b]
JSON 这个东西不行啊,咱还是不能搞定 ,能帮看下吗?
[b]问题补充:[/b]
晕,刚没注意看,我改了下,谁知道没改完 抱歉抱歉

  • 写回答

10条回答 默认 最新

  • iteye_521 2008-11-08 19:37
    关注

    我晕,行了,别再路径这个问题上费时间了,应该不是路径问题,你程序我感觉就不对,
    这个:
    [code="java"]File file1 = this.getFile(); [/code]得到的是file1,可是你却
    [code="java"]Util.write(path, file); [/code]这个file哪里来的啊??

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(9条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类