uploadify 2.1.0 java spring mvc 2003版excel 附件上传

来源:互联网 发布:万合天宜工作体验 知乎 编辑:程序博客网 时间:2024/06/07 15:14

上传页面uploadExcel.jsp代码:


 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
   
 <script src="http://localhost:8080/platform/js/jquery/jquery-1.7.2.min.js" type="text/javascript"></script>
 
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>上传excel</title>
    
  <link href="http://localhost:8080/platform/css/style.css" rel="stylesheet" type="text/css">
<!-- 引入uploadify -->
<link rel="stylesheet" type="text/css" href="http://localhost:8080/platform/js/uploadify/uploadify.css">
<script src="http://localhost:8080/platform/js/uploadify/swfobject.js" type="text/javascript"></script>
<script src="http://localhost:8080/platform/js/uploadify/jquery.uploadify.min.js" type="text/javascript"></script>



<script type="text/javascript"> 
 
//关闭窗口
function closeWindow(){
var api = frameElement.api, W = api.opener;
api.close();
//刷新父页面
W.queryData();
}
//取消上传
function cancelUpload(){
$('#uploadify').uploadifyClearQueue();
}
function uploadFiles() {
$('#uploadify').uploadifyUpload();

}
 
$(document).ready(function() {
     $("#uploadify").uploadify({
         'uploader': 'http://localhost:8080/platform/js/uploadify/uploadify.swf',
         'script': 'http://localhost:8080/platform/uploadFile.do',
         'cancelImg': 'http://localhost:8080/platform/js/uploadify/cancel.png',
         'folder': 'upload',
         'queueID': 'fileQueue',
         'auto': false,
         'multi': false,//是否支持多文件上传
         //'queueSizeLimit':2, //当允许多文件生成时,设置选择文件的个数,默认值:999 
        'sizeLimit':1024*1024*1,
        'fileExt':'*.xls;*.XLS',
        'fileDesc':'请选择2203版的excel文件',
        'buttonImg':'http://localhost:8080/platform/js/uploadify/select.gif',
        'onError':function(event,queueId,fileObj,errorObj){
       
        setTimeout(function() {
        $("#uploadify"+queueId).find(".percentage").html("- 文件过大");
        }, 1);
        },
        'onComplete':function(event,queueId,fileObj,response,data){
        alert(fileObj.name+" "+response);
        }
     });
 }); 
 
  
</script>
    
  </head>
  
  <body style="background-color:#FFFFFF">
<br><br>
<fieldset class="search_fieldset2">
<legend>上传附件</legend>
 
<form id="form1" runat="server">
       
        
</form>
<table width="96%" border="0" align="center" >
<tr>
    <td align="right" nowrap>选择要上传的文件:</td>
    <td align="left" > <input type="file" name="uploadify" id="uploadify" /><font color="red"> 每个文件大小需小于1M </font>
    <div id="fileQueue"></div>
    </td>
</tr>

 
<tr>
<td colspan="2">
<table border="0" width="100%">
<tr>
<td align="center" class="my_buttons"  >
        <input id="uploadBtn" type="button" value="上传" onclick="uploadFiles()"  />
        <input id="restBtn" type="button" value="取消上传" onclick="cancelUpload()" />
       <input id="restBtn" type="button" value="关闭" onclick="closeWindow()" />
 </td>
</tr>
</table>
</td>
  
</tr>
</table>
 
 
</fieldset>
 
 
 
  </body>
</html>



action /controller代码:FileAttachmentController.java


package com.platform.common.excel.action;


import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;


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;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import com.longtech.configuration.BaseConfiguration;
import com.platform.common.util.LoggerUtil;


/**
 * 
 * @Description: TODO
 * @author chen
 * @version 1.0, 
 * @date 2013-10-30 下午05:27:17
 */
@Controller
public class FileAttachmentController {

@RequestMapping(value = "/uploadFile.do")
public ModelAndView previewPieGraph(HttpServletRequest request,
HttpServletResponse response) throws IOException {
LoggerUtil.info(this.getClass(),"------uploadFile.do");

//"C:/upload/"; 指定上传的文件路径
String savePath = BaseConfiguration.getString("upload.root.path")+BaseConfiguration.getString("upload.file.excelData");


File f1 = new File(savePath);
//如果目录不存在 则 创建目录
if (!f1.exists()) {

f1.mkdirs();
}
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");
Writer out = response.getWriter();
try {
List fileList = upload.parseRequest(request);
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();
if (name == null || name.trim().equals("")) {
continue;
}
//扩展名格式:  
if (name.lastIndexOf(".") >= 0) {
extName = name.substring(name.lastIndexOf("."));
}

File file = null;
do {
//生成文件名:
name = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date()) +UUID.randomUUID().toString();
file = new File(savePath + name + extName);
} while (file.exists());

File saveFile = new File(savePath + name + extName);
//开始在 服务器中 写文件
item.write(saveFile);
}
}

out.write("上传成功");
} catch (FileUploadException e1) {
e1.printStackTrace();
out.write("上传失败:"+e1.getMessage());
} catch (Exception e) {
e.printStackTrace();
out.write("上传失败:"+e.getMessage());
}




out.flush();

return null;

}



}


原创粉丝点击