展开全部
你多少给点悬赏分啊!不然很少人愿意回答的
//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) {
}
}
//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) {
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询