首页>代码>终于搞定了stackoverflow的富文本编辑器pagedown>/wmd/src/main/java/com/javaniu/web/CommonController.java
package com.javaniu.web;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;
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.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/common")
public class CommonController {

	// 部署目录,修改为自己的
	private @Value("${deploy.path}")
	String deployPath;

	// 上传目录,修改为自己的
	private @Value("${upload.path}")
	String uploadPath;

	// 域名,修改为自己的
	private @Value("${javaniu.domain}")
	String javaniuDomain;

	@RequestMapping(value = { "test" }, method = { RequestMethod.GET })
	public ModelAndView upload(HttpSession session) {
		System.out.println("test get:" + session.getId());
		ModelAndView modelAndView = new ModelAndView("common/test");
		return modelAndView;
	}

	/**
	 * 通过apache commons.fileupload接收上传
	 * 
	 * @param file
	 * @param response
	 */
	@RequestMapping(value = { "upload" }, method = { RequestMethod.POST })
	public void upload(@RequestParam("file") CommonsMultipartFile file,
			HttpSession session, HttpServletResponse response) {
		System.out.println("upload post:" + session.getId());
		// 将该file放到session中以便客户端定时可以从session中获取到该对象的上传进度,不支持单客户端多文件上传,session会覆盖掉file
		// 要想支持多文件可以给客户端分配一个唯一标识即可,然后客户端请求时带上该标识从session中获取对应的对象即可,即:session.getAttribute(唯一标识)
		session.setAttribute("file", file);
		response.setContentType("text/html;charset=UTF-8");
		File uploadDir = new File(deployPath + uploadPath);
		if (!uploadDir.exists()) {// 不存在则创建
			uploadDir.mkdirs();
		}
		String name = System.currentTimeMillis() + ".jpg";
		String localPath = deployPath + uploadPath + name;
		String url = javaniuDomain + uploadPath + name;
		try {
			file.transferTo(new File(localPath));
			PrintWriter writer = response.getWriter();
			writer.print(url);
			writer.close();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取上传进度
	 * 
	 * @param file
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "process")
	public void process(HttpSession session, HttpServletResponse response) {
		System.out.println("process get:" + session.getId());
		// 上传进度百分比
		long processPercent = 0;
		CommonsMultipartFile file = (CommonsMultipartFile) session
				.getAttribute("file");
		if (file == null) {
			return;
		}
		long totalFileSize = file.getSize();
		long readedFileSize = file.getFileItem().getSize();

		System.out.println("totalFileSize:" + totalFileSize
				+ ",readedFileSize:" + readedFileSize);

		if (totalFileSize != 0) {
			processPercent = Math.round(readedFileSize / totalFileSize) * 100;
		}

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter writer;
		try {
			writer = response.getWriter();
			writer.print(processPercent);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
最近下载更多
wjh12345654321  LV14 2022年8月25日
wrg19970615  LV4 2019年11月12日
912299793  LV21 2018年7月13日
guolifeng  LV6 2017年9月30日
liangzai123  LV18 2017年1月3日
621726  LV15 2016年3月1日
zhudiyuan  LV13 2015年10月15日
samliao  LV13 2015年8月14日
神器神马  LV1 2015年8月11日
liutaols  LV8 2015年5月11日
最近浏览更多
君知否  LV17 2023年12月20日
whfuai  LV14 2023年5月11日
3029860453  LV2 2022年10月26日
wjh12345654321  LV14 2022年8月25日
纪凡琪  LV2 2022年7月12日
小五12345  LV13 2022年3月28日
Boss绝  LV8 2022年3月3日
dfkk_love  LV11 2021年12月24日
ls2008  LV15 2021年12月23日
哦入户很随意哦是  LV1 2021年12月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友