首页>代码>ssm框架开发视频检索下载项目管理后台>/video-search-system/src/java/com/ylwl/controller/ResouceAction.java
package com.ylwl.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
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.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.ylwl.pojo.Resource2;
import com.ylwl.pojo.User;
import com.ylwl.service.ResourceService;

import net.sf.json.JSONArray;

@Controller
@RequestMapping(value="/resource")
public class ResouceAction {
	@Resource
	private ResourceService resourceService;
	@ResponseBody
	@RequestMapping(value="/actionForList",produces = "application/json; charset=utf-8")
	public String actionForList(HttpServletRequest request){
		//String selector = request.getParameter("selector")==null?"":request.getParameter("selector");
		//String condition = request.getParameter("condition")==null?"":request.getParameter("condition");
		String mark = request.getParameter("mark2")==null?"":request.getParameter("mark2");
		String fluency = request.getParameter("fluency2")==null?"":request.getParameter("fluency2");
		String definition = request.getParameter("definition2")==null?"":request.getParameter("definition2");
		
		if(mark.equals("")&&fluency.equals("")&&definition.equals("")){
			List<Resource2> list = resourceService.queryForResourceList();
			int total = resourceService.queryForTotal();
			JSONObject jsonObject=new JSONObject();
			JSONArray jsonArray =new JSONArray();
			for(int i=0;i<list.size();i++){
				Resource2 resource2 = list.get(i);
				jsonArray.add(resource2);
			}
			jsonObject.put("total", total);
			jsonObject.put("rows", jsonArray);
			return jsonObject.toJSONString();
			
		}if (!mark.equals("")) {
			List<Resource2> list =resourceService.queryForResourceListByMark(mark);
			int total = resourceService.queryForTotalByMark(mark);
			JSONObject jsonObject=new JSONObject();
			JSONArray jsonArray =new JSONArray();
			for(int i=0;i<list.size();i++){
				Resource2 resource2 = list.get(i);
				jsonArray.add(resource2);
			}
			jsonObject.put("total", total);
			jsonObject.put("rows", jsonArray);
			return jsonObject.toJSONString();
		}if (!fluency.equals("")) {
			List<Resource2> list =resourceService.queryForResourceListByFluency(fluency);
			int total = resourceService.queryForTotalByFluency(fluency);
			JSONObject jsonObject=new JSONObject();
			JSONArray jsonArray =new JSONArray();
			for(int i=0;i<list.size();i++){
				Resource2 resource2 = list.get(i);
				jsonArray.add(resource2);
			}
			jsonObject.put("total", total);
			jsonObject.put("rows", jsonArray);
			return jsonObject.toJSONString();
		}
		if(!definition.equals("")) {
			List<Resource2> list =resourceService.queryForResourceListByDefinition(definition);
			int total = resourceService.queryForTotalByDefinition(definition);
			JSONObject jsonObject=new JSONObject();
			JSONArray jsonArray =new JSONArray();
			for(int i=0;i<list.size();i++){
				Resource2 resource2 = list.get(i);
				jsonArray.add(resource2);
			}
			jsonObject.put("total", total);
			jsonObject.put("rows", jsonArray);
			return jsonObject.toJSONString();
		}
		return null;
	}
	@RequestMapping(value="/actionForUploadFile",method=RequestMethod.POST)
	public String actionForUploadFile(@RequestParam(value="file") MultipartFile file, Resource2 resource,HttpServletRequest request){
		long fileS = file.getSize();
		String size = ""; 
		String path = request.getSession().getServletContext().getRealPath("/");  
		path+="\\upload";
		//String path="C:\\upload";
        String fileName = file.getOriginalFilename();  
        int pointIndex =  fileName.indexOf(".");  
        DecimalFormat df = new DecimalFormat("#.00"); 
        if (fileS < 1024) {
            size = df.format((double) fileS) + "BT";
        } else if (fileS < 1048576) {
            size = df.format((double) fileS / 1024) + "KB";
        } else if (fileS < 1073741824) {
            size = df.format((double) fileS / 1048576) + "MB";
        } else {
            size = df.format((double) fileS / 1073741824) +"GB";
        }
//        String fileName = new Date().getTime()+".jpg";  
        System.out.println(path);  
        String realRath="/video-search-system/upload/"+fileName;
        File targetFile = new File(path, fileName);  
        if(!targetFile.exists()){  
            targetFile.mkdirs();  
        }  
  
        //保存  
        try {  
            file.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
       // model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);  
        User user = (User)request.getSession().getAttribute("user");
        String userId = user.getUserId();
        resource.setUserId(userId);
        resource.setSize(size);
        resource.setResourcePath(realRath);
        resourceService.upLoadResource(resource);
        
		return "redirect:actionForResourceList";
		
	}
	
	@RequestMapping(value="/actionForResourceList")
	public String actionForResourceList(){
		return "list";
	}
	
	@ResponseBody
	@RequestMapping(value="/queryForResourceById")
	public Resource2 queryForResourceById(HttpServletRequest request){
		String videoId = request.getParameter("row");
		Resource2 resource=resourceService.queryForResourceById(videoId);
		return resource;
	}
	
	@ResponseBody
	@RequestMapping(value="/actionForDeleteResourceById",produces = "application/json; charset=utf-8")
	public String actionForDeleteResourceById(HttpServletRequest request){
		String videoId = request.getParameter("videoId");
		int index=resourceService.actionForDeleteResourceById(videoId);
		if (index==1) {
			return "200";
		}else {
			return "501";
		}
	}
	@RequestMapping(value="/actionForWatchVideo/{videoId}")
	public String actionForWatchVideo(@PathVariable String videoId,ModelMap map){
		Resource2 resource = resourceService.queryResourceById(videoId);
		map.addAttribute("url", resource.getResourcePath());
		return "watchPage";
		
	}
	@RequestMapping(value="/actionForDownLoadVideo/{fileName}/{type}")
	public void actionForDownLoadVideo(@PathVariable String type,@PathVariable String fileName, HttpServletRequest request,
			HttpServletResponse response) {
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("."+type);
		response.setHeader("Content-Disposition", "attachment;fileName="
				+ fileName);
		try {
			//String path = "C:\\upload";
			String path = request.getSession().getServletContext().getRealPath("/");  
			path+="\\upload";
			InputStream inputStream = new FileInputStream(new File(path
					+ File.separator + fileName));

			OutputStream os = response.getOutputStream();
			byte[] b = new byte[2048];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				os.write(b, 0, length);
			}

			 // 这里主要关闭。
			os.close();

			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
            //  返回值要注意,要不然就出现下面这句错误!
            //java+getOutputStream() has already been called for this response
	}
}
最近下载更多
不顾asdf  LV7 2023年11月1日
ssh123  LV10 2023年3月27日
1806795436  LV4 2023年1月9日
wangjialiang1  LV17 2022年6月6日
ddsfsf  LV2 2022年2月21日
wanglinddad  LV54 2022年2月19日
最代码-宋家辉  LV61 2021年7月8日
yg010917  LV2 2021年5月11日
浴皇大帝  LV8 2021年4月20日
dcy1234  LV1 2021年4月12日
最近浏览更多
15103432984  LV2 3月16日
李林112233  LV2 1月18日
1waxzsq212345  LV2 1月16日
XiaoSong888  LV2 1月10日
weilaizhisheng  LV21 2023年12月26日
马宇轩  LV1 2023年11月19日
不顾asdf  LV7 2023年11月1日
zhaozhiqi  LV5 2023年10月10日
czsczd  LV3 2023年9月1日
329512801  LV28 2023年7月19日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友