webuploader用java实现上传

发布于 2021-12-01 20:30:42 字数 220 浏览 826 评论 7

upload.js 中

        // 文件接收服务端。

        //server: 'http://webuploader.duapp.com/server/fileupload.php',

官网是php开发,怎么用java

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

空城仅有旧梦在 2021-12-05 16:50:58

你好 ,想知道用springmvc实现的webuploader怎么实现跨域呢

掩饰不了的爱 2021-12-05 16:27:46

Html代码  

沙与沫 2021-12-05 16:05:28

最佳答案?

清晨说ぺ晚安 2021-12-05 13:21:18

用帮助,点赞

落墨 2021-12-05 10:52:38

1.html

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>Uploader上传控件</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">
    <script src="/common/js/vendor/jquery.js"></script>
    
    <!-- include -->
    <script type="text/javascript" src="/common/plugin/webuploader/webuploader.js"></script>
    <link rel="stylesheet" type="text/css" href="/common/plugin/webuploader/webuploader.css">
    <!-- include -->
</head>
<body>
    <div id="uploader">
        <div class="queueList">
            <div id="dndArea" class="placeholder">
                <div id="filePicker"></div>
                <p>或将照片拖到这里,单次最多可选300张</p>
            </div>
        </div>
        <div class="statusBar" style="display:none;">
            <div class="progress">
                <span class="text">0%</span>
                <span class="percentage"></span>
            </div>
            <div class="info"></div>
            <div class="btns">
                <div id="filePicker2"></div>
                <div class="uploadBtn">开始上传</div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="/common/plugin/webuploader/webuploader.upload.js"></script>
    <script>
        // 实例化
        var uploader = WebUploader.create({
            pick: {
                id: '#filePicker',
                label: '点击选择图片'
            },
            formData: {
                uid: 123
            },
            dnd: '#dndArea',//拉拽区域div的id
            paste: '#uploader',//黏贴区域
            swf: '/common/plugin/webuploader/Uploader.swf',
            chunked: false,
            chunkSize: 512 * 1024,
            server: '/sys/uploadHeadPic/',//上传的URL
            // runtimeOrder: 'flash',
             accept: {
                 title: 'Images',
                 extensions: 'gif,jpg,jpeg,bmp,png,jar'
             },
            // 禁掉全局的拖拽功能。这样不会出现图片拖进页面的时候,把图片打开。
            disableGlobalDnd: true,
            fileNumLimit: 300,
            fileSizeLimit: 200 * 1024 * 1024,    // 200 M
            fileSingleSizeLimit: 50 * 1024 * 1024    // 50 M
        });
        /** 附件函数
        uploader.on( 'uploadSuccess', function( type ) {
           alert(11000);
        });
        */
    </script>
</body>
</html>

2.JAVA(我使用的是springmvc)这边是BaseAction

package com.tsou.comm.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import com.tsou.comm.util.StringUtil;

/**
 * 
 * <p class="detail">
 * 功能:公共Action
 * </p>
 * 
 * @ClassName: BaseAction
 * @version V1.0
 * @date 2014年9月25日
 * @author wangsheng
 */
public class BaseAction {
	private String allowSuffix = "jpg,png,gif,jpeg";//允许文件格式
	private long allowSize = 2L;//允许文件大小
	private String fileName;
	private String[] fileNames;
	
	public String getAllowSuffix() {
		return allowSuffix;
	}

	public void setAllowSuffix(String allowSuffix) {
		this.allowSuffix = allowSuffix;
	}

	public long getAllowSize() {
		return allowSize*1024*1024;
	}

	public void setAllowSize(long allowSize) {
		this.allowSize = allowSize;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String[] getFileNames() {
		return fileNames;
	}

	public void setFileNames(String[] fileNames) {
		this.fileNames = fileNames;
	}

	

	/**
	 * 
	 * <p class="detail">
	 * 功能:重新命名文件
	 * </p>
	 * @author wangsheng
	 * @date 2014年9月25日 
	 * @return
	 */
	private String getFileNameNew(){
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return fmt.format(new Date());
	}
	
	/**
	 * 
	 * <p class="detail">
	 * 功能:文件上传
	 * </p>
	 * @author wangsheng
	 * @date 2014年9月25日 
	 * @param files
	 * @param destDir
	 * @throws Exception
	 */
	public void uploads(MultipartFile[] files, String destDir,HttpServletRequest request) throws Exception {
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
		try {
			fileNames = new String[files.length];
			int index = 0;
			for (MultipartFile file : files) {
				String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
				int length = getAllowSuffix().indexOf(suffix);
				if(length == -1){
					throw new Exception("请上传允许格式的文件");
				}
				if(file.getSize() > getAllowSize()){
					throw new Exception("您上传的文件大小已经超出范围");
				}
				String realPath = request.getSession().getServletContext().getRealPath("/");
				File destFile = new File(realPath+destDir);
				if(!destFile.exists()){
					destFile.mkdirs();
				}
				String fileNameNew = getFileNameNew()+"."+suffix;//
				File f = new File(destFile.getAbsoluteFile()+"\"+fileNameNew);
				file.transferTo(f);
				f.createNewFile();
				fileNames[index++] =basePath+destDir+fileNameNew;
			}
		} catch (Exception e) {
			throw e;
		}
	}
	
	/**
	 * 
	 * <p class="detail">
	 * 功能:文件上传
	 * </p>
	 * @author wangsheng
	 * @date 2014年9月25日 
	 * @param files
	 * @param destDir
	 * @throws Exception
	 */
	public void upload(MultipartFile file, String destDir,HttpServletRequest request) throws Exception {
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
		try {
				String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
				int length = getAllowSuffix().indexOf(suffix);
				if(length == -1){
					throw new Exception("请上传允许格式的文件");
				}
				if(file.getSize() > getAllowSize()){
					throw new Exception("您上传的文件大小已经超出范围");
				}
				
				String realPath = request.getSession().getServletContext().getRealPath("/");
				File destFile = new File(realPath+destDir);
				if(!destFile.exists()){
					destFile.mkdirs();
				}
				String fileNameNew = getFileNameNew()+"."+suffix;
				File f = new File(destFile.getAbsoluteFile()+"/"+fileNameNew);
				file.transferTo(f);
				f.createNewFile();
				fileName = basePath+destDir+fileNameNew;
		} catch (Exception e) {
			throw e;
	}
}
}

3.写一个自己的Action继承BaseAction

package com.tsou.comm.controller;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.httpclient.HttpsURL;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tsou.comm.beans.TbSysArea;
import com.tsou.comm.beans.TbSysMenu;
import com.tsou.comm.beans.TbSysOrg;
import com.tsou.comm.beans.TbSysRole;
import com.tsou.comm.beans.TbSysUser;
import com.tsou.comm.service.SysAreaService;
import com.tsou.comm.service.SysUserService;
import com.tsou.comm.util.DateUtil;
import com.tsou.comm.util.LoginTicket;
import com.tsou.comm.util.StaticResouse;
import com.tsou.comm.util.StringUtil;

@Controller
@Scope(value="prototype")
@RequestMapping(value="/sys")
public class SysUserAction extends BaseAction{
	Logger log = Logger.getLogger(SysUserAction.class);

@RequestMapping("/uploadHeadPic")
	   public String uploadHeadPic(@RequestParam("file")MultipartFile file,HttpServletRequest request,HttpServletResponse response){
			try {
				super.upload(file, "/upload/user/",request);
				response.getWriter().print(super.getFileName());
			} catch (Exception e) {
				e.printStackTrace();
			}
			   return null;
	   }
}

哑剧 2021-12-05 06:24:21

标准的http上传而已.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文