日期:2014-05-16  浏览次数:20298 次

JSP上传文件
JSP上传文件时 要给表单加上enctype = multipart/form-data,并且method要为post

<form action="testUpload.jsp" method="post" enctype="multipart/form-data" name="form">
    账套:<input type="text" name="acid" value="${param.acid}"/><br>
    文件:<input type="file" name="fileName"><br>
    <button type="submit" name="提交" value="提交" onclick="validate()">提交</button>
</form>


表单参数要在JS里重新设置

function validate(){
    var accountId = document.form.acid.value;
    if(accountId == ""){
        alert("账套不能为空");
        return;
    }
    var url = "";
    var file1 = document.form.fileName.value;
    var oldAction = document.form.action;
    if(file1 == null || file1 == "")
    {
        alert('请选择需要上传的文件');
        return;
    }else
    {
        url = oldAction+"?fileName="+file1+"&acid="+accountId;
        alert(url);
        document.form.action=url;
    }
    document.form.submit();
}


这样在action的JSP里才能取到参数
<%
    String accountId = request.getParameter("acid");
    if(StringUtils.isBlank(accountId))accountId = "ac_50001";
    if(StringUtils.isNotBlank(accountId)){
    JSONObject account= IsoneERPEngine.accountService.getAccount(accountId);
    if(account == null){
        out.print("<font color=red>找不到对应的账套</font>");
        return;
    }
    String message =  SkuLedgerImport.importDoc(request,application);
    out.print(message);
}
%>