解决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;
}
}