java上传文件

FileUpload下载地址:
 
  http://commons.apache.org/fileupload/
 
  下载:commons-fileupload-1.2.2-bin.zip    得到:commons-fileupload-1.2.2.jar
 
  http://commons.apache.org/io/
 
  下载:commons-io-1.4-bin.zip       得到:commons-io-1.4.jar

思路:
1.上传的文件通过http协议指定格式传输到后台
method="post" action="upload.action" enctype="multipart/form-data"
2.后台获取文件数据 (可以限制上传文件大小upload.setSizeMax())
DiskFileItemFactory factory = new DiskFileItemFactory(); 
ServletFileUpload upload = new ServletFileUpload(factory); 
List<FileItem> items = upload.parseRequest(req); 
3.在指定目录下创建相同名称的文件然后将数据内容写入
String path = request.getServletContetxt().getRealPath("upload").getPath()+"\"+
+item.getName().substring(item.getName().lastIndexOf("/")+1);
item.write(new File(path));

 

 

 

[html]  view plain  copy
  1. <!--将文件作为一种数据格式;-->  
  2. <!--注意:1.method="post"  2.enctype="multipart/form-data"-->  
  3. <html>  
  4. <form id="form" name="form" method="post" action="upload.action" enctype="multipart/form-data">  
  5.   <table >  
  6.   <tr>  
  7.       <td >  
  8.       <input  type="file" style="font-size:14px" name="filename" />  
  9.       </td>  
  10.   </tr>  
  11.   <tr>  
  12.       <td >  
  13.       <input type="submit" style="font-size:14px" value="上传文件"  />  
  14.       </td>  
  15.   </tr>  
  16.   </table>  
  17.   </form>  
  18. </html>  


 

[java]  view plain  copy
  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import java.io.PrintWriter;  
  4. import java.util.List;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. import org.apache.commons.fileupload.FileItem;  
  10. import org.apache.commons.fileupload.FileUploadException;  
  11. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
  12. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
  13.   
  14.   
  15. public class UploadServlet extends HttpServlet {  
  16.   
  17.     @Override  
  18.     protected void service(HttpServletRequest req, HttpServletResponse resp)  
  19.             throws ServletException, IOException {  
  20.               
  21.             req.setCharacterEncoding("gbk");  
  22.             resp.setContentType("text/html;charset=gbk");  
  23.             PrintWriter out = resp.getWriter();  
  24.             //磁盘文件工厂类;  
  25.               DiskFileItemFactory factory = new DiskFileItemFactory();  
  26.               //关联关系创建ServletFileUpload类;  
  27.               ServletFileUpload upload = new ServletFileUpload(factory);  
  28.               List<FileItem> items;  
  29.             try {  
  30.               //将req中的数据放入表单域;  
  31.                 items = upload.parseRequest(req);  
  32.                 for(FileItem item : items){  
  33.                 //判断是否是文件  
  34.                     if(!item.isFormField()) {  
  35.                             //文件名称;  
  36.                             String filename = item.getName();  
  37.                             //前缀路径;  
  38.                             //某些浏览器会将文件的绝对路径全部提交;(ie)Y (chrome)N  
  39.                             filename = filename.substring(filename.lastIndexOf("\\")+1);  
  40.                             //在WebRoot下创建一个uploads文件夹;  
  41.                             //找到服务器上的路径upload文件夹;  
  42.                             File file = new File(req.getServletContext().getRealPath("uploads")+File.separator+filename);  
  43.                             //文件的二进制已经在item对象中,导入file文件;  
  44.                             item.write(file);  
  45.                             out.print("<html><center >上传成功,3秒后跳转!</center></html>");  
  46.                             return ;  
  47.                   }  
  48.                 }  
  49.             } catch (Exception e) {  
  50.                 e.printStackTrace();  
  51.             }  
  52.             out.print("<html><center>上传失败!</center></html>");  
  53.   
  54.     }   
  55. }  


web.xml

[html]  view plain  copy
  1. web.xml  
  2.    <servlet>  
  3.     <servlet-name>upload</servlet-name>  
  4.      <servlet-class>cn.lh.web.UploadServlet</servlet-class>  
  5.   </servlet>  
  6.   <servlet-mapping>  
  7.      <servlet-name>upload</servlet-name>  
  8.      <url-pattern>/upload.action</url-pattern>  
  9.   </servlet-mapping>  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值