📄 fileuploadaction.java
字号:
package org.appfuse.webapp.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.appfuse.Constants;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ValidationAware;
public class FileUploadAction extends BaseAction implements ValidationAware {
private File file;
private String fileContentType;
private String fileFileName;
private String name;
public String execute() throws Exception {
if (this.cancel != null) {
return "cancel";
}
// the directory to upload to
String uploadDir =
ServletActionContext.getServletContext().getRealPath("/resources") +
"/" + getRequest().getRemoteUser() + "/";
// write the file to the file specified
File dirPath = new File(uploadDir);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
//retrieve the file data
InputStream stream = new FileInputStream(file);
//write the file to the file specified
OutputStream bos = new FileOutputStream(uploadDir + fileFileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
// place the data into the request for retrieval on next page
getRequest().setAttribute("location", dirPath.getAbsolutePath()
+ Constants.FILE_SEP + fileFileName);
String link =
getRequest().getContextPath() + "/resources" + "/" +
getRequest().getRemoteUser() + "/";
getRequest().setAttribute("link", link + fileFileName);
return SUCCESS;
}
public String doDefault() {
return INPUT;
}
public void setFile(File file) {
this.file = file;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public File getFile() {
return file;
}
public String getFileContentType() {
return fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -