Java 上传下载的

1.上传的步骤:

a.导入SmartUpload.jar    b.创建一个上传的类的对象    c.初始化   d.上传至服务器   e.保存    注意:表单提交时需要指定enctype="multipart/form-data"(多数据类型提交)。

上传的jar包有SmartUpload.jar  和    fastupload-core-0.6.3.jar   可以在网上找一找,它们都可以实现伤上传的功能,我们就用SmartUpload.jar来试一试。

 

①创建一个Java  web 项目(用帕斯卡命名 就是首字母小写   列如:upioad,ownioad或者upioadOwnioad)

②导包SmartUpload.jar 放在WebRoot→WEB-INF→lib中

③创建一个Servlet的Java类实现HttpServlet接口  (用驼峰命名  就是每个首字母大写  列如:UpioadServlet, OwnioadServlet)

④上传部分的代码 (UpioadServlet)

  

package upioad;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartFile;

import com.jspsmart.upload.SmartFiles;

import com.jspsmart.upload.SmartUpload;

 

public class UploadServlet<Files> extends HttpServlet {

        public void doGet(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException {

                                        this.doPost(request, response);  

}

 public void doPost(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException {     

       //创建一个上传下载的对象   SmartUpload su=new SmartUpload();

  //初始化对象   su.initialize(getServletConfig(), request, response);   

    try {      

       //限制上传文件的后缀    

 su.setAllowedFilesList("txt,doc,jpg");

    //设置文件上传的大小限制    

   su.setMaxFileSize(1024*1024*2);//2M字节数   

     //上传文件到服务器    

     su.upload();   

     SmartFiles fs=su.getFiles();     

   su.save("/files/");   

     for(int i=0;i<fs.getCount();i++) {    

     SmartFile f=fs.getFile(i);  

      System.out.println(f.getFileExt());

        //判断f是否存在      

  if(f.isMissing()==false)    

     {    

      //文件存在.另存为  用时间的方式存储文件名 重复存储相同的文件,不被覆盖    

     f.saveAs("/files/"+System.currentTimeMillis()+f.getFileName());    

              }  

      }      

      //指定文件保存路径  

           su.save("/files");

        System.out.println("上传成功");

    request.getRequestDispatcher("../show.jsp").forward(request, response); //内部跳转

  } catch (Exception e) {  

  // TODO Auto-generated catch block

   request.setAttribute("eroor","文件格式不正确");   

 request.getRequestDispatcher("../index.jsp").forward(request, response); //内部跳转     

        }

 }

}

jsp  简单页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>  

<head>    

<base href="<%=basePath%>">   

      <title>上传页面</title>

 <meta http-equiv="pragma" content="no-cache">  

<meta http-equiv="cache-control" content="no-cache">  

<meta http-equiv="expires" content="0">  

   <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

 <meta http-equiv="description" content="This is my page">

 <!--  <link rel="stylesheet" type="text/css" href="styles.css">  -->

  </head>  

  <body>

    <form action="servlet/UploadServlet" method="post" enctype="multipart/form-data">  

     <input type="file"  name="myfile" ><br><br>

     <input type="submit" value="上传"/>${eroor }  

</form>  

</body>

</html>

⑤下载部分代码(OwnioadServlet)

 

package ownioad;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

 import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;

import com.jspsmart.upload.SmartUploadException;

public class DownLoad extends HttpServlet {

 

 public void doGet(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException {

      this.doPost(request, response);

   }

 public void doPost(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException {       

//获取要下载文件的名称

  String name =new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");     

  System.out.println(request.getParameter("name"));

     //创建一个上传下载的对象

  SmartUpload su=new SmartUpload();

  //初始化对象

  su.initialize(getServletConfig(), request, response);

  try {     

  //设置下载对象内容不自动打开   

 su.setContentDisposition(null);

   //调用下载的方法   

 su.downloadFile("/files/"+name);

   System.out.println("下载成功!");

  } catch (SmartUploadException e) {

   // TODO Auto-generated catch block    e.printStackTrace();

  }  

 }     

}

jsp  简单页面

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@page import="java.io.File"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>    

<base href="<%=basePath%>">       

  <title>My JSP 'show.jsp' starting page</title>   

   <meta http-equiv="pragma" content="no-cache">

 <meta http-equiv="cache-control" content="no-cache">  

<meta http-equiv="expires" content="0">   

 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

 <meta http-equiv="description" content="This is my page">

 <!--  <link rel="stylesheet" type="text/css" href="styles.css">  -->

  </head>

    <body>

  <%     File f =new File(application.getRealPath("/files"));   

  File []fs=f.listFiles();    

if(fs!=null && fs.length>0)  

   {     for(File file:fs)

{      out.print(file.getName());

 %>    

<a href="fig?name=<%=file.getName() %>">

<%=file.getName() %><br/></a>     

<%   

   }    

    }   

  %>    

</body>

</html>

 

 送大家一句话 ;代码虐 我千百遍 ,我视代码如初恋

 

转载于:https://www.cnblogs.com/zhaoxiangyu/p/7156653.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值