`

【SSI开发总结.8】Struts2中实现文件上传功能

 
阅读更多

1.编写上传表单

...............................

<s:form name="f1" action="upload!add.htm" method="post" enctype="multipart/form-data">

<s:file name="upload"/><input type="submit" name="Submit" value="上传" />

</s:form>

................................

1.编写Action

package action.game.editor.windows;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class UploadAction extends ActionSupport {
private File upload;//与表单文件域Name属性相同
private String uploadContentType;//表单文件域Name+"ContentType"
private String uploadFileName;//表单文件域Name+"FileName"

private String savePath="/uploadFiles";//保存路径
private String allowTypes="image/pjpeg,image/gif,image/bmp,image/x-png";//允许的文件类型

private String getSavePath() throws Exception
{
return ServletActionContext.getRequest().getRealPath(savePath);
}
//上传文件对应文件内容的setter
public void setUpload(File upload)
{
this.upload = upload;
}
//上传文件的文件类型的setter
public void setUploadContentType(String uploadContentType)
{
this.uploadContentType = uploadContentType;
}
public String getUploadContentType() {
return uploadContentType;
}
//上传文件的文件名的setter
public void setUploadFileName(String uploadFileName)
{
this.uploadFileName = uploadFileName;
}

//得到随机文件名
private String generateFileName(String fileName) {
DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
String formatDate = format.format(new Date());

int random = new Random().nextInt(10000);

int position = fileName.lastIndexOf(".");
String extension = fileName.substring(position);

return formatDate + random + extension;
}


public String execute(){

return SUCCESS;
}


//添加本地图片
public String addLocalIMG() throws Exception{
if(allowTypes.indexOf(uploadContentType)!=-1)
{
if(upload.length()<200000){
String realFileName=generateFileName(uploadFileName);
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + "//" + realFileName);
//以上传文件建立一个文件上传流
FileInputStream fis = new FileInputStream(upload);
//将上传文件的内容写入服务器
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0)
{
fos.write(buffer , 0 , len);
}
fos.close();
fis.close();
//url=ServletActionContext.getRequest().getContextPath()+"/uploadFiles/"+realFileName; //读取图片网络地址
return SUCCESS;
}
else
return "sizeError";
}
else
return "typeError";

}

}

3.注意,最好在struts配置文件中修改最大上传文件大小,然后,在程序中再对文件大小做限制,并且这个限制的值不能大于struts配置文件中已经设置的最大值,单位为字节

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
<include file="struts-default.xml"/>
<include file="struts-forward.xml"/>
<include file="struts-back.xml"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.multipart.maxSize" value="4000000" />
</struts>

注意,struts.multipart.maxSize默认为2M大小限制

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics