📄 uploadaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.upload.struts.action;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
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.util.MessageResources;
import com.upload.struts.form.UploadForm;
import java.util.Date;
/**
* MyEclipse Struts
* Creation date: 08-09-2007
*
* XDoclet definition:
* @struts.action input="/index.jsp" validate="true"
*/
public class UploadAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UploadForm upform=(UploadForm)form;
FormFile inputFile=upform.getFile();
String fileName=inputFile.getFileName();
try {
fileName = new String(fileName.getBytes("gb2312"),"gbk");//处理中文文件名的问题
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String filePath = request.getRealPath("/")+"load\\"+fileName;//取当前系统路径
try{
int max_size=8192;
if(inputFile.getFileSize()>max_size){
ActionErrors errors=new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("error.maxsize"));
saveErrors(request,errors);
return mapping.findForward("failure");
}
InputStream inputStream=inputFile.getInputStream();
byte[] fileContents=new byte[inputFile.getFileSize()];
inputStream.read(fileContents);
File outputFile=new File(filePath);
// System.out.print(filePath+"load\\"+fileName);
FileOutputStream fos=new FileOutputStream(outputFile);
fos.write(fileContents);
fos.close();
}catch(FileNotFoundException e){
e.printStackTrace();
ActionErrors errors=new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("error.io"));
saveErrors(request,errors);
return mapping.findForward("failure");
}catch(IOException ex){
ex.printStackTrace();
ActionErrors errors=new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("error.io"));
saveErrors(request,errors);
return mapping.findForward("failure");
}
request.setAttribute("filePath",filePath);
return mapping.findForward("success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -