まとめのアップロード

7940 ワード

index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix ="r" tagdir="/WEB-INF/tags/r" %>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<link href="<c:url value = "/res/js/upload/uploadify.css"/>" rel="stylesheet" type="text/css"/>
		<script type="text/javascript">
			var ROOT_PATH = '<c:url value="/" />';
			var JSESSIONID = "<%=request.getSession().getId()%>";
		</script>
		<script src="<c:url value='/res/js/jquery-1.9.1.min.js'/>"></script>
		<script type="text/javascript" src="<c:url value = "/res/js/upload/jquery.uploadify.min.js"/>"></script>
		<script type="text/javascript" src="<c:url value = "/res/js/upload.js"/>"></script>
		<script type="text/javascript" src="<c:url value = "/res/js/new.js"/>"></script>
		<title>upload file</title>
	</head>
	<body>
		<form action="<c:url value = "/upload"/>"  id="dataForm" method = "post" role="form" class="form-horizontal">
			<input type="hidden" name="attachment" id="attachment"/>
			<input type="file" name="uploadify" id="uploadify">
			<span class="help-block">*            </span>
			<div id="fileQueue"></div>
			<button type="button" class="btn btn-primary" id="submitBtn">  </button>
		</form>
	</body>
</html>
new.js
$(function() {
	//var fileTypeExts = "*.doc;";
	prepareUpload(ROOT_PATH+'upload/attachment;jsessionid=' + JSESSIONID, ROOT_PATH+'res/images/addFile.png');
	$("#submitBtn").click(function() {
		if($("#fileQueue").text() != '') {
			$('#uploadify').uploadify('upload','*');
		} else {
			alert("           !");
		}
	});
	/*$("#dataForm").validate({
		rules : {
			name : {
				required : true
			},
			"type.id" : {
				required : true
			}
		},
		errorElement : "span",
		messages : {
			name : {
				required : "       "
			},
			"type.id" : {
				required : "       "
			}
		}
	});*/
});

function onSelect(file) {
	var fileName = file.name;
	var dotIdx = fileName.lastIndexOf(".");
	var fileExt = fileName.substring(dotIdx + 1);
	setType(fileExt.toLowerCase());
	fileName = fileName.substring(0, dotIdx);
	$("#name").val(fileName);
}

var extMap = {
	"jpg": "IMAGE",
	"png": "IMAGE",
	"gif": "IMAGE",
	"bmp": "IMAGE",
	"avi": "VIDEO",
	"flv": "VIDEO",
	"mp4": "VIDEO",
	"rmvb": "VIDEO",
	"mp3": "AUDIO",
	"wma": "AUDIO",
	"wav": "AUDIO",
	"txt": "DOCUMENT",
	"doc": "DOCUMENT",
	"ppt": "DOCUMENT",
	"pdf": "DOCUMENT"
};
function setType(fileExt) {
	var type = extMap[fileExt];
	$("[data-code='"+type+"']").attr("selected", "true");
	$("#resourceTypeId").trigger("change");
	setTimeout(function() {
		$("[data-field-name='fileExt']").val(fileExt);
	}, 1000);
}
uipload.js
function prepareUpload(postUrl, buttonImage) {
	var fileTypeExts = "*.mp4;*.rmvb;*.mkv;*.zip;*.flv;*.xls;*.doc;*.xlsx;*.f4v;*.png;*.jpg;*.bmp;*.gif;*.mp3;*.wav;*.txt;*.doc;";
	/*var resourceType = $("#resourceType").val();
	if(resourceType!=""&&resourceType!=null){
		if(resourceType=="IMAGE"){
			fileTypeExts = "*.png;*.jpg;*.bmp;*.gif;";
		}else if(resourceType=="VIDEO"){
			fileTypeExts = "*.mp4;*.flv;*.f4v;";
		}else if(resourceType=="DOCUMENT"){
			fileTypeExts = "*.xls;*.doc;*.xlsx;";
		}
	}*/
	//alert(fileTypeExts);
    $("#uploadify").uploadify({
        'successTimeout' : 3000,
        'auto'           : false,
        'swf'            : ROOT_PATH+'res/js/upload/uploadify.swf',
        'uploader'       : postUrl,
        'queueID'        : 'fileQueue',
        'queueSizeLimit' : 1,
        'buttonImage'    : buttonImage,
        'fileTypeDesc'   : '    ',
        'fileTypeExts'   : fileTypeExts,
        'multi'          : false,
        'wmode'          : 'transparent',
        'onUploadSuccess': function(file, data, response) {
            //           ,                  
        	if(data.charAt(0) == "\"") {
        		data = data.substring(1);
        	}
        	if(data.charAt(data.length-1) == "\"") {
        		data = data.substring(0, data.length - 1);
        	}
        	var fileExt = data.substring(data.lastIndexOf(".") + 1);
        	$("#fileExt").val(fileExt);
            $("#attachment").val(data);
            $("#dataForm").submit();
        },
        'onUploadError':function(file, errorCode, errorMsg, errorString) {
        	
        },
        'onQueueComplete' : function(queueData) {
        	alert(queueData.uploadsSuccessful);
        },
        'onSelect':function(file) {
            onSelect(file);
        }
    });
}

controller:
package com.learning.upload.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Controller
@RequestMapping("/upload")
public class UploadController extends BasicController{

	@RequestMapping
	public String index(){
		return "/upload/index";
	}
	
	@RequestMapping(value = "/attachment", method = RequestMethod.POST)
	@ResponseBody
	public String upload(@RequestParam CommonsMultipartFile Filedata, HttpServletRequest request){
		//String name = Filedata.getFileItem().getName();
		String path = getFileUploadPath();
        String absolutePath = getAbsolutePath(path, request);
        File folder = new File(absolutePath);
		if(!folder.isDirectory()){
			try {
				folder.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			String rawName = Filedata.getFileItem().getName();
            String fileExt = rawName.substring(rawName.lastIndexOf("."));
            String newName = System.currentTimeMillis() + UUID.randomUUID().toString() + fileExt;
            File saveFile = new File(absolutePath + File.separator + newName);
			Filedata.getFileItem().write(saveFile);
			return path+"/"+rawName;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String save(@RequestParam String attachment){
		
		return "success";
	}
	
}