解决firefox3.0,ie8 在上传文件时只能获得文件名,而不能获得文件路径的问题
2009-01-09 09:13
661 查看
解决firefox3.0,ie8 在上传文件时只能获得文件名,而不能获得文件路径的问题:
我们的目的是要获取到文件的全路径,包括文件地址和文件名。下面我们以(struts 1.x)为例讲解
------------------------------------------------------------------------------------
1.jsp页面
<html:form action="importAction" method="post" enctype="multipart/form-data">
<html:file property="file"/>
<html:submit/>
</html:form>
注意:我们采用<html:file>标签来实现文件上传,该标签必须嵌套在<html:form>中,并且加上:enctype属性
在<html:file>中property属性名file 应该和actionform中FormFile类型的属性对应。
2. ActionForm
public class UploadActionForm extends ActionForm {
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
3.Action
public class UploadAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//获取客户端参数
UploadActionForm uaf = (UpaloadActioinForm) form;
FormFile file= uaf.getFile();
String fileFullAddress = copyFile(file);
//fileFullAddress就是我们说要的文件全路径(文件路径+文件名)
// 使用得到的文件全路径进行操作
}
//文件拷贝,返回的字符串为文件的路径+文件名
private String copyFile(FormFile file) throws IOException {
InputStream in = null;
OutputStream out = null;
String address = "D:/newFile.xml";
try {
in = new BufferedInputStream(file.getInputStream());
out = new BufferedOutputStream(new FileOutputStream(address));
int len;
while ((len = in.read()) != -1) {
out.write(len);
out.flush();
}
} catch (IOException e) {
log.error("文件创建出错!");
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
}
return address;
}
}
我们的目的是要获取到文件的全路径,包括文件地址和文件名。下面我们以(struts 1.x)为例讲解
------------------------------------------------------------------------------------
1.jsp页面
<html:form action="importAction" method="post" enctype="multipart/form-data">
<html:file property="file"/>
<html:submit/>
</html:form>
注意:我们采用<html:file>标签来实现文件上传,该标签必须嵌套在<html:form>中,并且加上:enctype属性
在<html:file>中property属性名file 应该和actionform中FormFile类型的属性对应。
2. ActionForm
public class UploadActionForm extends ActionForm {
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
3.Action
public class UploadAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//获取客户端参数
UploadActionForm uaf = (UpaloadActioinForm) form;
FormFile file= uaf.getFile();
String fileFullAddress = copyFile(file);
//fileFullAddress就是我们说要的文件全路径(文件路径+文件名)
// 使用得到的文件全路径进行操作
}
//文件拷贝,返回的字符串为文件的路径+文件名
private String copyFile(FormFile file) throws IOException {
InputStream in = null;
OutputStream out = null;
String address = "D:/newFile.xml";
try {
in = new BufferedInputStream(file.getInputStream());
out = new BufferedOutputStream(new FileOutputStream(address));
int len;
while ((len = in.read()) != -1) {
out.write(len);
out.flush();
}
} catch (IOException e) {
log.error("文件创建出错!");
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
}
return address;
}
}
相关文章推荐
- IE8上传文件时获取文件本地路径问题(C:\fakepath\……)的解决办法
- 解决IE8上传文件时javascript取文件的本地路径的问题C:\fakepath\..
- QNetworkAccessManager 实现FTP上传功能,解决 文件名是中文,QUrl含有中文路径 不能上传的问题
- 解决IE8上传文件时javascript取文件的本地路径的问题C:\fakepath\..
- IE8上传文件时获取文件本地路径问题(C:\fakepath\……)的解决办法
- IE8上传文件时获取文件本地路径问题(C:\fakepath\……)的解决办法
- IE8上传文件时javascript取文件的本地路径的问题C:\fakepath\
- centos linux 安装FTP服务器解决win不能上传带linux服务器上文件的问题
- IE8上传文件时文件本地路径变成"C:\fakepath\"的问题
- 关于post文件上传不能返回路径问题的解决方案
- IE8上传文件时javascript取文件的本地路径的问题C:/fakepath/
- IE8上传文件时读取文件的本地路径的问题
- JSP实现文件上传,解决上传文件名为乱码问题
- paip.解决中文url路径的问题图片文件不能显示
- IE7,IE8,上传文件控件input file和asp.net FileUpload控件无法获取完整路径的解决办法
- 解决ueditor上传文件到tomcat虚拟路径等问题
- file解决IE8中文件上传报typeerror拒绝访问问题的解决方法
- 【java】[文件上传jar包]commons-fileUpload组件解决文件上传(文件名)乱码问题
- 解决fstream不能打开带有中文路径文件的问题
- 2003上传文件不能超过200k问题解决方法问题