【问题标题】:How to Get URL Perameter or Form Data into JSP Multipart File Upload如何将 URL 参数或表单数据获取到 JSP 多部分文件上传中
【发布时间】:2015-06-14 11:11:20
【问题描述】:

我正在使用 Multipart 进行文件上传。 我需要在 servlet 中获取“pono”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>

这是小服务程序

    /*
 * 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 参数?

【问题讨论】:

    标签: jsp servlets file-upload multipartform-data


    【解决方案1】:
    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
                        }
                    }
                }
    

    【讨论】:

    • 在给定的代码中。 //在这里你可以获取文件类型可以使用我的多部分现有代码在这个if循环中上传????
    猜你喜欢
    • 2013-08-24
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2015-03-13
    相关资源
    最近更新 更多