以供自己备忘。
jsp 页面
注意下面的黑体部分
- <body>
- <html:form action="upload" enctype="multipart/form-data">
- <html:file property="file"></html:file>
- <html:submit></html:submit>
- </html:form>
- </body>
下面是formBean 主要是封装一个file
- public class UpLoadForm extends ActionForm {
- /**
- *
- */
- private static final long serialVersionUID = -5778426661470052057L;
- private FormFile file ;
- public FormFile getFile() {
- return file;
- }
- public void setFile(FormFile file) {
- this.file = file;
- }
- }
下面action 里面的代码
- package upload.action;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- 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.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.upload.FormFile;
- import upload.form.UpLoadForm;
- public class UpLoadAction extends Action {
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- if (form instanceof UpLoadForm) {
- String encoding = request.getCharacterEncoding();
- if(encoding!=null&&encoding.equalsIgnoreCase("utf-8"))
- {
- response.setContentType("text/html;charset=gb2312");
- }
- UpLoadForm tf = (UpLoadForm) form ;
- FormFile file = tf.getFile(); // get the upload file
- String contentType = file.getContentType();
- String size = file.getFileSize()+"byte"; // the upload size
- String fn = file.getFileName(); // get the upload file name
- try {
- InputStream stream = file.getInputStream(); // read the file
- String path = this.getServlet().getServletContext().getRealPath("/"); // get the save path
- // OutputStream ops = new FileOutputStream(this.checkFileExist(path+"upload")+"/"+fn); // 放在自己建的目录下,新建的目录是唯一的
- OutputStream ops = new FileOutputStream(path+"upload"+"/"+fn); // 放在这个目录下
- int br = 0;
- byte[] bf = new byte[8192];
- while ((br=stream.read(bf, 0, bf.length))!=-1) {
- ops.write(bf,0,br);
- }
- ops.close();
- stream.close();
- request.setAttribute("contentType", contentType);
- request.setAttribute("size", size);
- request.setAttribute("fn", fn);
- return mapping.findForward("display");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- System.out.println("not found the upload file");
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("io exception ");
- }
- }
- return null;
- }
- /**
- * 创建自己存放上传文件的文件夹
- * @param str
- * @return filePath 文件夹的路径
- */
- private String getOwnFilePath(String str)
- {
- String s = "";
- try {
- File ownFilePath = new File(str);
- File ofp2 = new File(str+"(1)");
- if(ownFilePath.exists())
- {
- ownFilePath.renameTo(ofp2);
- ownFilePath.mkdir();
- s = ownFilePath.getPath();
- }
- else
- {
- ownFilePath.mkdir();
- s = ownFilePath.getPath();
- }
- } catch (Exception e)
- {
- e.printStackTrace();
- System.out.println("errory cause by create filePath");
- }
- return s;
- }
- /**
- * 判断要创建的文件夹是否存在<br>
- * 存在 就在此文件夹名后面加(1)<br>
- * 直到没有重复名称为止,返回可用的那个
- * @param str 要创建的路径和文件名
- * @return file
- */
- private static File checkFileExists(String str)
- {
- File fp = new File(str) ;
- boolean flag = fp.exists();
- String path = str; // 根据需要可以将此路径返回去
- for(;flag;)
- {
- path = fp.toString()+"(1)";
- fp = new File(path);
- flag = fp.exists();
- }
- return fp ;
- }
- /**
- * 判断要创建的文件夹是否存在<br>
- * 存在 就在此文件夹名后面加(1)<br>
- * 直到没有重复名称为止,返回可用的那个
- * @param str 要创建的路径和文件名
- * @return file
- */
- private static String checkFileExist(String str)
- {
- File fp = new File(str) ;
- boolean flag = fp.exists();
- String path = str; // 根据需要可以将此路径返回去
- for(;flag;)
- {
- path = fp.toString()+"(1)";
- fp = new File(path);
- flag = fp.exists();
- }
- try {
- fp.mkdir();
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- return path ;
- }
- }
下面是struts-cfg.xml中的部分配置
- <form-bean name="uploadForm" type="upload.form.UpLoadForm" />
- <global-exceptions />
- <global-forwards />
- <action-mappings >
- <!-- upload file -->
- <action
- path="/upload"
- name="uploadForm"
- type="upload.action.UpLoadAction">
- <forward name="display" path="/upload/display.jsp" />
- </action>