(一)单个文件的上传步骤:

1.拷贝jar包:commons-fileupload.jar,  commons-io.jar 

下载链接(文件上传.rar):http://www.cnblogs.com/withyou/admin/Files.aspx

2.JSP页面中增加form:(form属性是固定的)

<form action= "uploadAction"    enctype="multipart/form-data" method="post"  >
      姓名:<input type="text" name="uname"  /><br/>
       附件:<input type="file" name="fileUpload"  /><br/>           
               <input type="submit"  value="提交" />
</form>

3.action层代码:

 1 public class UploadAction {
 2     private String uname;
 3     private  File fileUpload;
 4     private String   fileUploadFileName;//名字规范  File属性的 名字+FileName,(该属性为上传过来的文件名)
 5     public String execute() throws IOException{
 6         String path = ServletActionContext.getServletContext().getRealPath("/fileUpload/");//该path为tomcat下的webapp/工程/下
 7         for(int i = 0;i<fileUpload.length;i++){
 8             String[] fielNameArray = fileUploadFileName[i].split("\\.");
 9             //将上传的文件存到指定路径下,UUID是为了避免文件名重复
10             FileUtils.copyFile(fileUpload[i],new File(path+"\\"+UUID.randomUUID()+"."+fielNameArray[fielNameArray.length-1]));            
11         }
12         return null;
13     }
14     public String getUname() {
15         return uname;
16     }
17 
18     public void setUname(String uname) {
19         this.uname = uname;
20     }
21 
22     public File[] getFileUpload() {
23         return fileUpload;
24     }
25 
26     public void setFileUpload(File[] fileUpload) {
27         this.fileUpload = fileUpload;
28     }
29 
30     public String[] getFileUploadFileName() {
31         return fileUploadFileName;
32     }
33 
34     public void setFileUploadFileName(String[] fileUploadFileName) {
35         this.fileUploadFileName = fileUploadFileName;
36     }
37 }

4.补充几个java获取路径的信息
  a.获取web服务器下的文件路径 (tomcat等服务器安装目录/webapp/工程/)
    request.getRealPath("/") ;application.getRealPath("")【jsp中 】 ;ServletContext().getRealPath("") 

    如:request.getRealPath("web.xml") 的结果为:
    C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\strutsTest\web.xml 

  b.获取本地路径

    this.getClass().getClassLoader().getResource("").getPath(); ==/D:/workspace/strutsTest/WebRoot/WEB-INF/classes/ 
    this.getClass().getResource("").getPath().toString(); ==/D:/workspace/strutsTest/WebRoot/WEB-INF/classes/bl/ 

  c.获取相对路径 :

    request.getContextPath();  

 

(二)多个文件上传步骤:

 1.拷贝jar包;

 2.jsp代码如下:

  

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">   
11     <title>My JSP 'upload.jsp' starting page</title>
12     <script src="js/jquery-1.4.4.js"></script>
13     <script type="text/javascript">
14             /*添加附件*/
15             function addFileButton(){
16                 var fileStr = "<div>附件:<input type='file'  name='fileUpload' />  <input type='button' value='删除' onclick='delFileButton(this);' /><br/></div>";
17                 $("#div1").append(fileStr);
18             }
19             /*删除附件*/
20             function delFileButton(obj){
21                 obj.parentNode.parentNode.removeChild(obj.parentNode);
22             }
23     </script>
24   </head>
25   <body>
26            <form action= "uploadAction"    enctype="multipart/form-data"  method="post"  >
27                姓名:<input type="text" name="uname"  /><br/>
28                <fieldset>
29                    <legend><input type="button"  value="添加附件"  onclick="addFileButton();" /></legend>
30                    <div  id="div1">
31                        <div>附件:<input type="file" name="fileUpload"  /><input type="button" value="删除" onclick="delFileButton(this);" /><br/></div>
32                    </div>
33                </fieldset>
34                <input type="submit"  value="提交" />
35            </form>
36   </body>
37 </html>
View Code

相关文章:

  • 2021-05-26
  • 2022-01-07
  • 2022-01-07
  • 2021-10-17
  • 2022-01-07
猜你喜欢
  • 2022-01-07
  • 2021-11-28
  • 2021-12-21
  • 2022-12-23
  • 2022-01-07
相关资源
相似解决方案