标签:on() download font stack 调用 eth byte method tty
为了上传文件必须将表单的method设置为POST,将 enctype 设置为 muiltipart/form-data,只有设置为这种情况下,浏览器才会把用户选择文件的二进制数据发送给服务器。
? ?
public class UploadAction extends ActionSupport {
? ?
/**
*
*/
private static final long serialVersionUID = 283051583917637792L;
? ?
private File upload;
private String uploadFileName;
private String uploadContentType;
private String uploadPath;
? ?
public UploadAction() {
uploadPath = "upload";
}
? ?
@Override
public String execute() throws Exception {
? ?
if (upload == null) {
addActionError("没有选择上传文件");
return INPUT;
}
? ?
String filePath = ServletActionContext.getServletContext().getRealPath(uploadPath);
? ?
java.io.File dir = new java.io.File(filePath);
if (dir.exists() == false) {
if (dir.mkdirs() == false) {
addActionError("创建目录失败,目录路径=" + filePath);
return INPUT;
}
}
? ?
System.out.println("Upload FileName =" + uploadFileName);
System.out.println("Upload ContentType =" + uploadContentType);
? ?
FileOutputStream fileOutputStream = new FileOutputStream(filePath + File.pathSeparator + uploadFileName);
FileInputStream fileInputStream = new FileInputStream(upload);
byte[] buffer = new byte[4096];
int len = 0;
do {
len = fileInputStream.read(buffer, 0, buffer.length);
if (len > 0) {
fileOutputStream.write(buffer, 0, len);
}
} while (len > 0);
? ?
addActionMessage("上传完成,保存路径=" + filePath + File.pathSeparator + uploadFileName);
? ?
return SUCCESS;
}
? ?
public File getUpload() {
return upload;
}
? ?
public void setUpload(File upload) {
this.upload = upload;
}
? ?
public String getUploadFileName() {
return uploadFileName;
}
? ?
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
? ?
public String getUploadContentType() {
return uploadContentType;
}
? ?
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
}
<action name="upload" class="org.drsoft.actions.file.UploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/png</param>
<param name="maximumSize">102400</param>
</interceptor-ref>
<!--必须增加该项,如果不增加其他的拦截器会默认增加该拦截器-->
<interceptor-ref name="defaultStack"/>
? ?
<result name="success">/WEB-INF/content/file/upload.jsp</result>
<result name="input">/WEB-INF/content/file/upload.jsp</result>
</action>
? ?
? ?
? ?
标签:on() download font stack 调用 eth byte method tty
原文地址:http://www.cnblogs.com/li3807/p/6353860.html