如何将URL参数或表单数据导入JSP多部分文件上载

时间:2015-04-09 07:35:48

标签: jsp servlets file-upload multipartform-data

我使用Multipart进行文件上传。 我需要得到" pono" servlet中的url参数。怎么弄它

文件上传JSP

<form method="post" action="../Upload?pono= <%=request.getParameter("pono")%>" enctype="multipart/form-data" onsubmit="submit.disabled = true;
            submit.value = 'Processing ..';
            return true;">
        <table width="100%" border="1" cellpadding="5" cellspacing="0"  style="border: thin #999999">
            <tr>
                <td>
                    <input type="file" name="file" id="file"  multiple="multiple"/>
                </td>
                <td>
                    <input type="submit" id="submit" name="submit" value="Upload"/></td>
            </tr>
        </table>
</form>

这是servlet

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import Bean.FileBean;
import Dao.FileDao;
import Logic.DBmanager;
import Logic.GetMethod;
import java.io.File;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.multipart.MultipartParser;
import com.oreilly.servlet.multipart.Part;
import com.oreilly.servlet.multipart.FilePart;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.logging.Logger;

/**
 *
 * @author 02948
 */
public class Upload extends HttpServlet {

    private String fileSavePath;
    private static final String UPLOAD_DIRECTORY = "attachment";
    static final Logger logger = Logger.getLogger(Upload.class.getName());
    public static Connection con = DBmanager.GetConnection();
    PrintWriter out = null;
    private String uname = "";
    private String db_path = "";

    @Override
    public void init() {
        /*save uploaded files to a 'attachment' directory in the web app*/
        fileSavePath = getServletContext().getRealPath("/") + File.separator + UPLOAD_DIRECTORY;
        if (!(new File(fileSavePath)).exists()) {
            // creates the directory if it does not exist  
            (new File(fileSavePath)).mkdir();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
        response.setContentType("text/html;charset=UTF-8");
        out = response.getWriter();
        HttpSession session = request.getSession(true);
        uname = session.getAttribute("uid").toString();
        int MaxfSize = 1024 * 1024 * 50; //50MB
        int i = 1;
        MultipartParser m = new MultipartParser(request, MaxfSize);  /* file limit size of 50MB*/

        Logic.GetMethod g = new GetMethod();
        Part p;
        while ((p = m.readNextPart()) != null) {
            if (p.isFile()) {
                FilePart fPart = (FilePart) p;  // get some info about the file
                String name = fPart.getFileName();
                if (name != null) {
                    fPart.writeTo(new File(fileSavePath));
                    i++;
                    db_path = "attachment" + "\\" + fPart.getFileName();
                    FileBean fb = new FileBean();
                    fb.setATT_ID(g.Get_Seq("ATT_ID", "CBA_ATT_MST"));
                    fb.setATT_TYPE("ATT");
                    fb.setPO_NO("0");
                    fb.setBILL_NO("0");
                    fb.setF_PATH(db_path);
                    fb.setF_NAME(fPart.getFileName());
                    fb.setUSER_ID(uname);
                    FileDao fdo = new FileDao();
                    fdo.addFileRecord(fb);
                } else {
                }
            }
        }
    }
}

如何使用url从jsp传递的pono perameter?

1 个答案:

答案 0 :(得分:0)

write down following code:


            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setFileSizeMax(MAX_FILE_SIZE);
            upload.setSizeMax(MAX_REQUEST_SIZE);

            List<FileItem> formItems = upload.parseRequest(request);
            if (formItems != null && formItems.size() > 0) {
                for (FileItem item : formItems) {
                    if (!item.isFormField()) {
                         FilePart fPart = (FilePart) p;  // get some info about the file
            String name = fPart.getFileName();
            if (name != null) {
                fPart.writeTo(new File(fileSavePath));
                i++;
                db_path = "attachment" + "\\" + fPart.getFileName();
                FileBean fb = new FileBean();
                fb.setATT_ID(g.Get_Seq("ATT_ID", "CBA_ATT_MST"));
                fb.setATT_TYPE("ATT");
                fb.setPO_NO("0");
                fb.setBILL_NO("0");
                fb.setF_PATH(db_path);
                fb.setF_NAME(fPart.getFileName());
                fb.setUSER_ID(uname);
                FileDao fdo = new FileDao();
                fdo.addFileRecord(fb);
                    } else {
                        //you can get other parameter except file type
                    }
                }
            }