文件上传控件UpLoad的大小怎么写代码??如上传大小为50M

谢谢大家了!!!如允许客户端上传的文件大小为50M... 谢谢大家了!!!
如允许客户端上传的文件大小为50M
展开
 我来答
百度网友4d858dc
2010-10-03
知道答主
回答量:36
采纳率:0%
帮助的人:30.2万
展开全部
你多少给点悬赏分啊!不然很少人愿意回答的
//Created by MyEclipse Struts
//XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.0/xslt/JavaClass.xsl

package testfile.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import org.apache.struts.upload.*;

import testfile.forms.*;
import java.io.*;

/**
* MyEclipse Struts
* Creation date: 11-14-2005
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class UploadFileAction extends Action{

public ActionForward execute (
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{

ActionErrors errors=new ActionErrors();

UploadFileForm hff=(UploadFileForm)form;
FormFile file=hff.getFile();

//*************如果ActionForm中没验证则加上这段*****************
//if(file==null || file.getFileSize()<10 || file.getFileName()==null){
// errors.add("file",new ActionError("error.file.null"));
// saveErrors(request,errors);
// return (new ActionForward(mapping.getInput()));
//}

//*************取得上传文件的信息****************
String dir=servlet.getServletContext().getRealPath("/upload");
String fname=file.getFileName();
String size=Integer.toString(file.getFileSize())+"bytes";
String url=dir+"\\"+fname;

//*************限制非图片文件的上传*******************
String fileType=(fname.substring(fname.lastIndexOf('.')+1)).toLowerCase();
if((!fileType.equals("jpg")) && (!fileType.equals("bmp")) && (!fileType.equals("gif")) && (!fileType.equals("jpeg"))){
errors.add("filetype",new ActionError("error.file.type"));
saveErrors(request,errors);
return (new ActionForward(mapping.getInput()));
}

//*************限制文件不能超过2M******************
if(file.getFileSize()>2097152){
errors.add("size",new ActionError("error.file.size"));
saveErrors(request,errors);
return (new ActionForward(mapping.getInput()));
}

//*************要用到的输入输出流*****************
InputStream streamIn=file.getInputStream();
OutputStream streamOut=new FileOutputStream(url);

//*************将文件输出到目标文件*****************
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=streamIn.read(buffer,0,8192))!=-1){
streamOut.write(buffer,0,bytesRead);
}

streamOut.close();
streamIn.close();
file.destroy();

return mapping.findForward("sucess");
}

}

package testfile.forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionError;

import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;

/**
* MyEclipse Struts
* Creation date: 11-14-2005
*
* XDoclet definition:
* @struts.form name="uploadForm"
*/
public class UploadFileForm extends ActionForm {

private FormFile file=null;
//private String fname;
//private String size;
//private String url;

public FormFile getFile(){
return file;
}
public void setFile(FormFile file){
this.file=file;
}
//**************验证文件如果为空则返回*****************
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {

ActionErrors errors=new ActionErrors();

if(file==null || file.getFileSize()<5 || file.getFileName()==null){
errors.add("file",new ActionError("error.file.null"));
}

return errors;
}

public void reset(ActionMapping mapping, HttpServletRequest request) {

}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式