禅之禅的gravatar头像
禅之禅 2016-03-24 19:06:05

struts开发组件自动上传文件如何传入表单参数?

在struts 项目中 ,想在jsp 页面用组件(如:uploadify)自动上传文件(如:图片,表格等)时,传入form 表单,

后台 action 接收时用 ActionForm form作为参数,然后直接调用 struts 的封装 上传的方法:

List<UploadFile> uploadFileList = super.uploadFile(form, false);

这样 就能直接 获取要上传的文件了:

PS:拜托大神了,限 今晚,在线等 QQ:541059135

所有回答列表(1)
小夕酱的gravatar头像
小夕酱  LV13 2016年3月25日

1.FileUploadServlet源代码

 

package com.oryx.upload;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;

 

public class FileUploadServlet extends HttpServlet {

    public FileUploadServlet() {
        super();
    }

    public void destroy() {
        super.destroy(); 
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        RequestContext context = new ServletRequestContext(request);

        String tmpdir = "d:/tmp/";//所放位置
      if (FileUpload.isMultipartContent(context)) {//判断request中是否包含"multipart/form-data"
            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setRepository(new File(tmpdir));//设置临时文件路径
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setSizeMax(400000000);

            List items = new ArrayList();
            try {
                items = upload.parseRequest(request);
            } catch (FileUploadException e) {
                System.out.println("文件上传发生错误" + e.getMessage());
            }

            Iterator it = items.iterator();
            while (it.hasNext()) {
                FileItem fileItem = (FileItem) it.next();
                if (fileItem.isFormField()) {
                    //判断FileItem类对象封装的数据是否属于一个普通表单字段,还是属于一个文件表单字段,
                    //如果是普通表单字段则返回true,否则返回false。
                    System.out.println("FieldName:"
                            + fileItem.getFieldName()
                            + " Name:"
                            + fileItem.getName()
                            + " Code:"
                            + new String(fileItem.getString().getBytes(
                                    "iso8859-1"), "utf-8"));
                } else {
                    System.out.println("FieldName:" + fileItem.getFieldName()
                            + " Name:" + fileItem.getName() + " IsInMemory:"
                            + fileItem.isInMemory() + " ContentType:"
                            + fileItem.getContentType() + " Size:"
                            + fileItem.getSize());

                    if(fileItem.getName()!= null&&fileItem.getSize()!=0){
                        File srcFile = new File(fileItem.getName());//源文件
                        File destFile = new File("d:/temp/"+srcFile.getName());//目标文件
                        try {
                            fileItem.write(destFile);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }else{
                        System.out.println("没有选择文件或文件内容为空!");
                    }
                }
            }
            String url = "/oryx/jsp/util/sucess.jsp";
            response.sendRedirect(url);
        }

    }

    public void init() throws ServletException {
        // Put your code here
    }

}

2.upload.jsp源代码


<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
  <head>
    <title>文件上传2</title>
  </head>
<%
  String contextPath = request.getContextPath();
%>
  <body>
 <FORM name="form1" action="<%=contextPath%>/com/oryx/upload/FileUploadServlet" enctype="multipart/form-data" method="POST">
   选择文件:<input type="file" name="myfile" />
   <input type="submit" value="上传">
  </FORM>
 </body>
</html>

注:enctype="multipart/form-data" 一定要写。

 

3.在Web.xml中配置Servlet

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
  <servlet>   
    <servlet-name>FileUploadServlet</servlet-name>
    <servlet-class>com.oryx.upload.FileUploadServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>FileUploadServlet</servlet-name>
    <url-pattern>/com/oryx/upload/FileUploadServlet</url-pattern>
  </servlet-mapping>
</web-app>

评论(4) 最佳答案
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友