struts2中使用属性驱动的形式上传文件

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yinbucheng/article/details/51146712

username:<s:textfield name="fileUploadTools.username"></s:textfield>

<br>

<s:file name="fileUploadTools.uploadFile"></s:file>

<br>

<s:file name="fileUploadTools.uploadFile"></s:file>

<br>

<s:file name="fileUploadTools.uploadFile"></s:file>

<br>


控制层Register.java


public class Register extends ActionSupport{

    private FileUploadTools fileUploadTools = new FileUploadTools();

   setter....

   getter.....


   public String execute() throws IOException{

      fileUploadTools.beginUpload();

     return "register";


    }


}



public FileUploadTools{

     private String username;

     private File uploadFile[];//上传的文件是数组类型

     private String uploadFileFileName[];//文件名是数组类型

     private String uploadFileContentType[];

     setter.....

    getter.......

   

    public String beginUpload() throws IOException{

         String targetDirectory = ServletActionContext.getRequest().getRealPath("/uplaod");

         for(int i=0;i<uploadFile.length;i++){

           File target = new File(targetDirectory,new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()).toString()+System.nanoTime+uploadFileFileName[i]);

           FileUtils.copyFile(uploadFile[i],target);

       }

           return "register";

  }

}



其中的主要变化为name的属性值加入fileUploadTools,该值与action的属性名称要一一对应,而且必须一致。

private FileUploadTools fileUploadTools = new FileUploadTools();

展开阅读全文

struts2使用Servlet上传文件

05-11

实现了ProgressListener的类.主要用来实现获取上传文件进度条rn[code=Java]rnpublic class myProgressListener implements ProgressListener rn private HttpSession session;rnrn public myProgressListener(HttpServletRequest req) rn System.out.println("存入session");rn session=req.getSession();rn FileUploadStatus status = new FileUploadStatus();rn session.setAttribute("status", status);rn rnrnrn public void update(long pBytesRead, long pContentLength, int pItems) rn // TODO Auto-generated method stubrn System.out.println("执行了update方法");rn FileUploadStatus status = (FileUploadStatus) session.getAttribute("status");rn status.setPBytesRead(pBytesRead);rn status.setPContentLength(pContentLength);rn status.setPItems(pItems);rn rn[/code]rnrnprogressServlet 在页面上用ajax调用该方法获取上传文件进度rn[code=Java]rnpublic void doPost(HttpServletRequest request, HttpServletResponse response) rn HttpSession session = request.getSession();rn FileUploadStatus status = (FileUploadStatus) session.getAttribute("status");rn try rn response.reset();rn response.getWriter().write("\"pBytesRead\":"rn +status.getPBytesRead()+",\"pContentLength\":"+status.getPContentLength()+"");rn System.out.println("\"pBytesRead\":"rn +status.getPBytesRead()+",\"pContentLength\":"+status.getPContentLength()+"");rn catch (IOException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn rn public void doGet(HttpServletRequest request, HttpServletResponse response) rn this.doPost(request,response);rn rn rn[/code]rnrnuploadServlet 用上来传文件..rn[code=Java]rnprotected void doGet(HttpServletRequest req, HttpServletResponse resp)rn throws ServletException, IOException rn // TODO Auto-generated method stubrn this.doPost(req, resp);rn rnrn @Overridern protected void doPost(HttpServletRequest req, HttpServletResponse resp)rn throws ServletException, IOException rn DiskFileItemFactory factory = new DiskFileItemFactory();rn factory.setSizeThreshold(2048*1024);rn myProgressListener getBarListener = new myProgressListener(req);rn ServletFileUpload upload = new ServletFileUpload(factory);rn upload.setProgressListener(getBarListener);rn try rn //在这里调用upload.parseRequest(req)获取不到文件rnrn List formList = upload.parseRequest(req);rn Iterator formItem = formList.iterator();rn rn while (formItem.hasNext()) rn FileItem item = (FileItem) formItem.next();rn if (item.isFormField()) rn System.out.println("Field Name:" + item.getFieldName());rn else rn String fileName = item.getName().substring(item.getName().lastIndexOf("\\")+1);rn File file = new File("e:\\temp"rn + "\\" + fileName);rn System.out.println("e:\\temp"rn + "\\" + fileName);rn OutputStream out = item.getOutputStream();rn InputStream in = item.getInputStream();rn req.getSession().setAttribute("outPutStream", out);rn req.getSession().setAttribute("inPutStream", in);rn item.write(file);rn rn rn rn catch (FileUploadException e2) rn // TODO Auto-generated catch blockrn e2.printStackTrace();rn catch (Exception e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn[/code]rnrnrnWEB.XMLrn[code=XML]rnrn rn struts2rn rn org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterrn rn rn struts2rn /*rn rnrnrn rn rn CharacterEncodingFilterrn com.anv.filter.CharacterEncodingFilterrn rn charSetrn utf8rn rn rn enabledrn truern rn rn rn CharacterEncodingFilterrn /*rn rnrn rn uploadrn com.anv.videoFile.uploadServletrn rn rn progressServletrn com.anv.videoFile.progressServletrn rn rn uploadrn /upload.servletrn rn rn progressServletrn /getBar.servletrn rn[/code]rnrn3个类单独使用的时候,完全正常.上传进度条等可以实现rn当放在带有struts2的工程中使用rnList formList = upload.parseRequest(req);rn获取不到值..而request中上传文件的值也有.rn我想应该是struts2的拦截器的问题.但是自己找了两天 还是找不到解决的方法rn希望这里有高人指点一二 论坛

struts2 上传文件 问题

05-10

一、 怎么设置弹出新对话框啊?也就是 _blank属性rn二、拿来孙鑫的 Struts2 深入详解,抄了代码 运行时没报错,可是查看上传文件的文件夹中没有发现对应上传的文件,奇怪了! 我用的是MyEclipse7.0 。好奇怪啊。返回的页面上 fileFileName dir都有,不过fileContextType 显示不出来.rnrn请高手帮忙啊!!!!rnrn具体代码:rnrnpackage tea;rnrnimport com.opensymphony.xwork2.ActionSupport;rnimport java.io.BufferedInputStream;rnimport java.io.BufferedOutputStream;rnimport java.io.File;rnimport java.io.FileInputStream;rnimport java.io.FileOutputStream;rnimport java.io.IOException;rnimport java.util.Date;rnimport javax.servlet.ServletContext;rnimport org.apache.struts2.ServletActionContext;rnpublic class LoadPicAction extends ActionSupport rn private File file;rn private String fileFileName;rn private String fileContextType;rn private String uploadDir;rn rnrn public File getFile() rn return file;rn rnrn public void setFile(File file) rn this.file = file;rn rnrn public String getFileFileName() rn return fileFileName;rn rnrn public void setFileFileName(String fileFileName) rn this.fileFileName = fileFileName;rn rnrn public String getFileContextType() rn return fileContextType;rn rnrn public void setFileContextType(String fileContextType) rn this.fileContextType = fileContextType;rn rnrn public String getUploadDir() rn return uploadDir;rn rnrn public void setUploadDir(String uploadDir) rn this.uploadDir = uploadDir;rn rnrn public void validate() rn if(fileFileName==null || fileFileName.equals(""))rn this.addFieldError("form1", "请选择上传照片!");rn rn rn rn rn public String execute() throws Exception rn this.setUploadDir("/tphoto");rn String path=ServletActionContext.getServletContext().getRealPath(uploadDir);rn File dir=new File(path);rn if(!dir.exists())dir.mkdir();rn rn BufferedOutputStream bos=null;rn BufferedInputStream bis=null;rn tryrn FileInputStream fis=new FileInputStream(file);rn bis=new BufferedInputStream(fis);rn rn FileOutputStream fos=new FileOutputStream(new File(dir,this.getFileFileName()));rn bos=new BufferedOutputStream(fos);rn rn byte[] buf=new byte[4096];rn rn int len=-1;rn while((len=bis.read(buf))!=-1)rn bos.write(buf, 0, len);rn rn rn catch (IOException e) rn this.addFieldError("file", "上传照片失败!");rn return INPUT;rn rn finallyrn tryrn if(null!=bis)bis.close();rn rn catch(IOException e)rn e.printStackTrace();rn rn rn tryrn if(null!=bos)bos.close();rn rn catch(IOException e)rn e.printStackTrace();rn rn rn return SUCCESS;rn rnrn rn rn rnrn 论坛

没有更多推荐了,返回首页