用于解決在一個頁面上傳多個文件的問題.
<script type="text/javascript">
function showDialog(url){
?//彈出一個showModalDialog,并以returnValue來獲取返回值
?var returnValue = window.showModalDialog(url);
?//alert(returnValue);
?if(returnValue!=null){
? //for(var i=0;i<returnValue.length;i++){
?? //document.all.info.innerHTML = returnValue[i]+"<br>";
? //}
? //輸出返回值
? document.getElementById("IMAGENAME").value = document.getElementById("IMAGENAME").value + returnValue + "|";
? //document.all.imgname.innerHTML=returnValue;
?}
}
</script>

在示例圖中的"上傳圖片"是一個button按鈕,給它一個onclick事件,當點擊時觸發:
<td height="25">
?????????<input name="IMAGENAME" type="text" id="IMAGENAME" />
?????????<input type="button" name="Submit3" value="上傳圖片" onclick="showDialog('automobile/up.jsp')" />
</td>
彈出圖片上傳頁:

圖片上傳頁:
<base target="_self"/>
<body>
<form action="/qcbst/v_uploadautomobileimage" name="myform" method="post" enctype="multipart/form-data" >
<table width="100%"? border="0" align="center">
? <tr>
??? <td colspan="2">圖片上傳</td>
? </tr>
? <tr>
??? <td width="30%"><div align="right">汽車圖片:</div></td>
??? <td width="70%"><input type="file" name="imgname" size="20"><input type="submit" value="上傳"></td>
? </tr>
? <tr>
??? <td><div align="right">上傳信息:</div></td>
??? <td> </td>
? </tr>
</table>
</form>
</body>
當點擊上傳后.圖片上傳到服務器,然后,關閉當前頁,返回圖片的名稱給調用頁:
上傳servlet:
public void doGet(HttpServletRequest request, HttpServletResponse response)
???throws ServletException, IOException {
??response.setCharacterEncoding("GBK");
??response.setContentType("text/html");
??UploadBean up = new UploadBean();//--------文件上傳類
??String path = request.getRealPath(request.getContextPath()).substring(0,request.getRealPath(request.getContextPath()).lastIndexOf("\\"));
????
???? try{
???????? File file = new File(path+"/img/automobile");//
???????? if(!file.exists()){
?????????? file.mkdir();
???????? }
???? }catch(Exception e){???? ?
???? }
???? up.setObjectPath(path+"/img/automobile/");//
???? //設置上傳文件大小
???? up.setSize(10000*1024);
???? //可上傳文件后綴名
???? up.setSuffix(".JPG.JPGE.jpg.jpge.gif.GIF");
???? try{
???? ?up.setSourceFile(request);
???? }catch(Exception e){
???? ?response.getWriter().write("圖片上傳失敗!!!");
???? }
???? String [] saSourceFile = up.getSourceFile();
???? String [] saObjectFile = up.getObjectFileName();
???? String [] saDescription = up.getDescription();
????
???? int iCount = up.getCount();
???? String sObjectPath = up.getObjectPath();
????
???? if(saDescription != null && saDescription[0].indexOf("ERR") == -1){
????? response.getWriter().write("<script language='javascript'>window.returnValue = '");
????? response.getWriter().write(saObjectFile[0]);
????? response.getWriter().write("';window.close();</script>");
???? }else{
???? ?
???? ?response.getWriter().write("圖片上傳失敗!!!");
???? }
?}
?上傳完成后:
OK,完成了!!!