用smartUpload上传下载文件

我的代码:

Up.jsp

<%@ page language="java"contentType="text/html"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>Insert titlehere</title>

<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">

</head>

<body>

<h2>上传实例</h2>

<form action="upload?method=1"method="post"  enctype="multipart/form-data">

    <input type="file" name="file"size="10"><br>

    <input type="file" name="file2"size="10"><br>

    <input type="submit" name="submit"value="提交">

</form>

<br/>

<br/>

<h2>下载实例</h2>

<a href="upload?method=2&down=en&fileName=a.jpg">下载“风云争霸”图片:a.jpg</a><br><br>

<a href="upload?method=2&down=ch&fileName=风云争霸.jpg">下载“风云争霸”图片:风云争霸.jpg</a><br><br>

</body>

</html>

upload.Java/Servlet

package cn.up;

 

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Date;

import java.util.UUID;

 

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import com.jspsmart.upload.File;

import com.jspsmart.upload.Files;

import com.jspsmart.upload.SmartUpload;

import com.jspsmart.upload.SmartUploadException;

 

 

public class upload extends HttpServlet {

 

    SmartUpload su ;

    protected void doGet(HttpServletRequest request, HttpServletResponseresponse) throws ServletException, IOException {

       request.setCharacterEncoding("UTF-8");

       int method=Integer.parseInt(request.getParameter("method"));

       su= new SmartUpload();

       su.initialize(this.getServletConfig(), request, response);//初始化,此项必须

       if(method==1){

           upload(request, response);

       }else if(method==2){

           download(request, response);

       }else{

           //出错了

       }

             

    }

 

 

    private void download(HttpServletRequest request,

           HttpServletResponse response) {

       //设定ContentDispositionnull以禁止浏览器自动打开文件

       //response.setCharacterEncoding("UTF-8");

       su.setContentDisposition(null);

       String filepath="/manage/cover/";

       try {

       //获得请求参数               

           String down=request.getParameter("down");//与下面取值比较一下,与中文传值的区别

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

           System.out.println(fileName);

           //得到文件在服务器中的路径

           filepath=filepath+fileName;

           //判断文件名是否有中文

           if(down.equals("en")){//此处也可以进行正则表达式,判断提交的文件名是否由字母和数字组成。从而选择不同的下载处理方式

              System.out.println("下载2");

              su.downloadFile(filepath);            

           }else{

              byte[] b=fileName.getBytes();

              char[]c=new char[b.length];

              for(int x=0;x<b.length;x++)c[x]=(char)(b[x]&0x00FF);

              fileName=new String(c);

              su.downloadFile(filepath,"image/jpeg",fileName);

           }

       } catch (Exception e) {

              e.printStackTrace();

           }

    }

 

 

    private void upload(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {

       Integer maxID = 432;

       String result = "上传的照片格式和大小有问题,上传照片失败!";

       String type = null;

       String imageType[] = { "JPG", "jpg", "gif", "bmp", "BMP" };

       String filedir = "/manage/cover/";

       long maxsize = 2 * 1024 * 1024; // 设置每个上传文件的大小,为2MB

       try {

          

           su.setMaxFileSize(maxsize); // 限制上传文件的大小

           su.upload(); // 上传文件

           Files files = su.getFiles(); // 获取所有的上传文件

           for (int i = 0; i < files.getCount(); i++) { // 逐个获取上传的文件

              File singlefile = files.getFile(i);

              type = singlefile.getFileExt();

              System.out.println("提交成功2");

              for (int ii = 0; ii < imageType.length; ii++) {

                  if (imageType[ii].equals(type)) {

                     if (!singlefile.isMissing()) { // 如果选择了文件

                         Date time=new Date();

                         String timename=UUID.randomUUID().toString();

                         System.out.println(timename);

                         filedir = filedir + timename + "."

                                + singlefile.getFileExt();

                            singlefile.saveAs(filedir, File.SAVEAS_VIRTUAL);

                            result = "上传照片成功!";

                            System.out.println(result);

                     }

                  }

              }

           }

       } catch (Exception e) {

           e.printStackTrace();

       }

       //request.getRequestDispatcher("").forward(request,response);

    }

 

 

    protected void doPost(HttpServletRequest request, HttpServletResponseresponse) throws ServletException, IOException {

       doGet( request, response);

    }

}


刚开始,jsp页面提交数据出现Files' name is invalid or does not exist (1205)错误,百度后解决了:

设置form中的属性enctype 为"multipart/form-data"

如:<formaction="/company/servlet/AddNewsManage" method="post" enctype="multipart/form-data"name="form1" onSubmit="return isNoNull()">


表单中enctype="multipart/form-data "的意思, 是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data ,才能完整的传递文件数据,进行下面的操作.
enctype="multipart/form-data"是上传二进制数据; form里面的input的值以2进制的方式传过去。
form里面的input的值以2进制的方式传过去,所以request就得不到值了。 也就是说加了这段代码,用request就会传递不成功,取表单值加入数据库时,用到下面的:

SmartUploadsu = new SmartUpload();//新建一个SmartUpload对象

su.getRequest().getParameterValues();取数组值

su.getRequest().getParameter( );取单个参数单个值


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值