struts2 文件上传路径错误 ERROR [STDERR] java.io.FileNotFoundException:
struts2上传文件到系统根目录,但是一直报路径错误下面是我写的代码importjava.io.File;importjava.io.FileInputStream;i...
struts2上传文件到系统根目录,但是一直报路径错误
下面是我写的代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.baosight.bocs.spring.service.ImportExcelService;
import com.opensymphony.xwork2.ActionSupport;
public class ImportExcelAction extends ActionSupport {
//上传文件集合
private List<File> file;
//上传文件名集合
private List<String> fileFileName;
//get和set方法就不写了
public get....
public set....
// 执行上传功能
private void uploadFile(int i) throws FileNotFoundException, IOException {
try {
InputStream in = new FileInputStream(file.get(i));
String dir = ServletActionContext.getRequest().getRealPath("/upload");
File uploadFile = new File(dir, this.getFileFileName().get(i));
OutputStream out = new FileOutputStream(uploadFile);
byte[] buffer = new byte[1024 * 1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
然后执行代码后报出的错误
14:21:51,281 ERROR [STDERR] java.io.FileNotFoundException: D:\JAVA\jboss-4.2.0.CR2\server\default\.\deploy\bocs.war\upload\2.14.xls (系统找不到指定的路径。)
14:21:51,281 ERROR [STDERR] at java.io.FileOutputStream.open(Native Method)
14:21:51,281 ERROR [STDERR] at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
14:21:51,281 ERROR [STDERR] at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
14:21:51,281 ERROR [STDERR] at com.baosight.bocs.struts.action.ImportExcelAction.uploadFile(ImportExcelAction.java:79)
14:21:51,281 ERROR [STDERR] at com.baosight.bocs.struts.action.ImportExcelAction.execute(ImportExcelAction.java:68)
14:21:51,281 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14:21:51,281 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
14:21:51,281 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
14:21:51,281 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:589)
我知道是路径不对 但是为什么getRealPath方法得不到正确的绝对路径啊,我用的是ISS框架(Ibatis;Spring;Struts2)
正确的路径是:
D:\JAVA\jboss-4.2.0.CR2\server\default\deploy\bocs.war\upload\2.14.xls
getRealPath方法给我的路径却是:
D:\JAVA\jboss-4.2.0.CR2\server\default\.\deploy\bocs.war\upload\2.14.xls
其中多出了一个“\.\”的目录 没想通这个目录是怎么多出来的 展开
下面是我写的代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.baosight.bocs.spring.service.ImportExcelService;
import com.opensymphony.xwork2.ActionSupport;
public class ImportExcelAction extends ActionSupport {
//上传文件集合
private List<File> file;
//上传文件名集合
private List<String> fileFileName;
//get和set方法就不写了
public get....
public set....
// 执行上传功能
private void uploadFile(int i) throws FileNotFoundException, IOException {
try {
InputStream in = new FileInputStream(file.get(i));
String dir = ServletActionContext.getRequest().getRealPath("/upload");
File uploadFile = new File(dir, this.getFileFileName().get(i));
OutputStream out = new FileOutputStream(uploadFile);
byte[] buffer = new byte[1024 * 1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
然后执行代码后报出的错误
14:21:51,281 ERROR [STDERR] java.io.FileNotFoundException: D:\JAVA\jboss-4.2.0.CR2\server\default\.\deploy\bocs.war\upload\2.14.xls (系统找不到指定的路径。)
14:21:51,281 ERROR [STDERR] at java.io.FileOutputStream.open(Native Method)
14:21:51,281 ERROR [STDERR] at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
14:21:51,281 ERROR [STDERR] at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
14:21:51,281 ERROR [STDERR] at com.baosight.bocs.struts.action.ImportExcelAction.uploadFile(ImportExcelAction.java:79)
14:21:51,281 ERROR [STDERR] at com.baosight.bocs.struts.action.ImportExcelAction.execute(ImportExcelAction.java:68)
14:21:51,281 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14:21:51,281 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
14:21:51,281 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
14:21:51,281 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:589)
我知道是路径不对 但是为什么getRealPath方法得不到正确的绝对路径啊,我用的是ISS框架(Ibatis;Spring;Struts2)
正确的路径是:
D:\JAVA\jboss-4.2.0.CR2\server\default\deploy\bocs.war\upload\2.14.xls
getRealPath方法给我的路径却是:
D:\JAVA\jboss-4.2.0.CR2\server\default\.\deploy\bocs.war\upload\2.14.xls
其中多出了一个“\.\”的目录 没想通这个目录是怎么多出来的 展开
5个回答
展开全部
把路径取出来 用字符串方法处理一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
\.\明显多了,检查代码去了就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
斜杠和反斜杠?路径好好看看~~~自己都找到错了,就顺着错误去找路径,然后修改~!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
uploadFile(int i) 在哪传入的值啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询