java版-JQuery上传插件Uploadify使用实例

http://bijian1013.iteye.com/blog/1993215
这里写图片描述

maven

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lzh</groupId>
    <artifactId>activitiDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency> 
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
            <version>7.0.62</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

    </dependencies>
</project>

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="/activitiDemo/css/uploadify.css"/>
<script type="text/javascript" src="/activitiDemo/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="/activitiDemo/js/swfobject.js"></script>
<script type="text/javascript" src="/activitiDemo/js/jquery.uploadify.min.js"></script>

</head>
<body>
    name:<input id="name" name="name">
    文件上传: <div id="file_upload"></div>
    <button id="btu" onclick="btu()"> 提交</button>
</body>
<script>
    $(function(){
        $("#file_upload").uploadify({
            height        : 30,
            swf           : './js/uploadify.swf',
            uploader      : '/activitiDemo/uploadFile.action',
            width         : 120
        });
    })
</script>
</html>

java

package up;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@WebServlet("/uploadFile.action")
public class UploadFile extends HttpServlet{

     private static final long serialVersionUID = 1L;  
     private String uploadPath = "D://xxx"; // 上传文件的目录  
     private File file;  
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            String savePath = req.getContextPath();     
            savePath = uploadPath+savePath + "/uploads/"; 
            File f1 = new File(savePath);  
            System.out.println(savePath);  
            if (!f1.exists()) {  
                f1.mkdirs();  
            } 
            DiskFileItemFactory fac = new DiskFileItemFactory(); 
            ServletFileUpload upload = new ServletFileUpload(fac);  
            upload.setHeaderEncoding("utf-8"); 
            List<FileItem> fileList = null;  
            try {  
                fileList = upload.parseRequest(req);  
            } catch (FileUploadException ex) {  
                return;  
            }  
            Iterator<FileItem> it = fileList.iterator();  
            String name = "";  
            String extName = "";

            while(it.hasNext()){
                FileItem item = it.next();
                if(!item.isFormField()){
                     name = item.getName(); 
                     long size = item.getSize();  
                     String type = item.getContentType(); 
                     System.out.println(size + " " + type);  
                     if (name == null || name.trim().equals("")) {  
                         continue;  
                     } 
                    // 扩展名格式:  
                     if (name.lastIndexOf(".") >= 0) {  
                         extName = name.substring(name.lastIndexOf("."));  
                     } 
                     do {  
                         // 生成文件名:  
                         name = UUID.randomUUID().toString();  
                         file = new File(savePath + name + extName);  
                     } while (file.exists()); 
                     try {  
                         item.write(file);
                     } catch (Exception e) {  
                         e.printStackTrace();  
                     } 
                }
            }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(req, resp);
    }
    public String getUploadPath() {
        return uploadPath;
    }
    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
    }


}

注意:

这里的核心是,通过前端插件传过来之后,用 org.apache.commons.fileupload包下面的DiskFileItemFactory,ServletFileUpload这个类来实现,通过是不是普通字段的方法item.isFormField()来得到是否是文件,然后有个write的方法,写到指定文件中。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发疯的man

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值