本测试有两个模块,一个是文件上上传,一个是文件下载,文件下载的时候会检查是否足有权限,如果没有,就会转发到登录页面,如果有权限,就会直接启动下载程序,给浏览器一个输出流。
下面直接上我的代码:
登录表单
<body>
<form id="form1" name="form1" method="post" action="login.action">
<p>
<label for="textfield">账号</label> <input type="text" name="username"
id="textfield" />
</p>
<p>
<label for="textfield2">密码</label> <input type="text" name="password"
id="textfield2" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" /> <br />
</p>
</form>
</body>
上传表单:
<body>
<form action="upload.action" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p>
<label for="fileField"></label>
<input type="file" name="fileobj" id="fileField" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" />
<br />
</p>
</form>
<a href="showpic.action">查看上传照片</a>
</body>
查看文件列表:
<table cellspacing="5" align="center">
<tr>
<s:iterator value="#request.photoList" id="photo" status="stu">
<td><a
href="downpic.action?inputFile=<s:property value="photo"/>"> <img
src='images/<s:property value="photo"/>' width="400" height="420">
</a>
</td>
</tr>
<tr>
</s:iterator>
</tr>
</table>
下面是action部分:
package com.updown.Action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private File fileobj;
private String fileobjFileName;
private String fileobjContentType;
/**
* @return
*/
public String execute() throws Exception {
InputStream is = new FileInputStream(fileobj);
String photoPath = ServletActionContext.getServletContext()
.getRealPath("/images");
File filePhotoPath = new File(photoPath);
if (!filePhotoPath.isDirectory()) {
filePhotoPath.mkdir();
}
String extension = FilenameUtils.getExtension(this.getFileobjFileName());
String filename = UUID.randomUUID().toString() + "." + extension;
File tofile = new File(photoPath, filename);
OutputStream os = new FileOutputStream(tofile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();
return SUCCESS;
}
public File getFileobj() {
return fileobj;
}
public void setFileobj(File fileobj) {
this.fileobj = fileobj;
}
public String getFileobjFileName() {
return fileobjFileName;
}
public void setFileobjFileName(String fileobjFileName) {
this.fileobjFileName = fileobjFileName;
}
public String getFileobjContentType() {
return fileobjContentType;
}
public void setFileobjContentType(String fileobjContentType) {
this.fileobjContentType = fileobjContentType;
}
}
package com.updown.Action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private String username;
private String password;
/**
* @return
*/
public String execute() {
// TODO Auto-generated method stub
if (username.equals("admin")&&password.equals("admin")){
ServletActionContext.getRequest().getSession().putValue("user", "admin");
return SUCCESS;
}
return INPUT;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.updown.Action;
import java.io.File;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ShowPicAction extends ActionSupport {
private String inputFile;
private InputStream targetFile;
public void setTargetFile(InputStream targetFile) {
this.targetFile = targetFile;
}
public InputStream getTargetFile(){
return ServletActionContext.getServletContext().getResourceAsStream("/images/"+inputFile);
}
/**
* @return
*/
public String execute() {
String photoPath = ServletActionContext.getServletContext()
.getRealPath("/images");
File fphotoPath = new File(photoPath);
String[] photoList = fphotoPath.list();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("photoList", photoList);
return SUCCESS;
}
public String downfile(){
String user = (String) ServletActionContext.getRequest().getSession().getAttribute("user");
if (user!=null&&user.equals("admin"))
return SUCCESS;
return LOGIN;
}
public String getInputFile() {
return inputFile;
}
public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}
}
struts-xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<default-action-ref name="download"></default-action-ref>
<action name="login" class="com.updown.Action.UserAction">
<result name="success">/upload.jsp</result>
<result name="input">/login.jsp</result>
</action>
<action name="upload" class="com.updown.Action.UploadAction">
<result name="success">/upload.jsp</result>
</action>
<action name="showpic"
class="com.updown.Action.ShowPicAction">
<result name="success">/index.jsp</result>
</action>
<action name="downpic" class="com.updown.Action.ShowPicAction"
method="downfile">
<result name="success" type="stream">
<param name="contentType">
application/octet-stream
</param>
<param name="inputName">targetFile</param>
<param name="bufferSize">4096</param>
<param name="contentDisposition">
filename="download.file"
</param>
</result>
<result name="login">/login.jsp</result>
</action></package></struts>
版权声明:本文为博主原创文章,未经博主允许不得转载。