Struts2实现单文件上传 (二)

2014-11-24 09:56:17 · 作者: · 浏览: 6
c void setFile(File file) {
this.file = file;
}

public String getFileFileName() {
return fileFileName;
}

public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}

public String getFileContentType() {
return fileContentType;
}

public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}

@Override
public String execute() throws Exception {

String uploadPath = ServletActionContext.getServletContext()
.getRealPath("/upload");

InputStream is = new FileInputStream(file);
OutputStream os = new FileOutputStream(new File(uploadPath,
this.fileFileName));

int length = 0;
byte[] buffer = new byte[1024];
while (-1 != (length = is.read(buffer))) {
os.write(buffer, 0, length);
}
is.close();
os.close();

return SUCCESS;
}
}

上传成功后的页面uploadResult.jsp:

[html]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>







upload successfully!

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>







upload successfully!


最后配置一下struts.xml

[html]
< xml version="1.0" encoding="UTF-8" >
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">









/uploadResult.jsp


< xml version="1.0" encoding="UTF-8" >
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">




/uploadResult.jsp