package downloadTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.log4j.Logger; public class FileLoadClient { private static Logger logger = Logger.getLogger(FileLoadClient.class); /**
* 文件上传
* @param url
* @param file
* @return
*/
public static String fileload(String url, File file) {
String body = "{}"; if (url == null || url.equals("")) {
return "参数不合法";
}
if (!file.exists()) {
return "要上传的文件名不存在";
} PostMethod postMethod = new PostMethod(url); try {
// FilePart:用来上传文件的类,file即要上传的文件
FilePart fp = new FilePart("file", file);
Part[] parts = { fp }; // 对于MIME类型的请求,httpclient建议全用MulitPartRequestEntity进行包装
MultipartRequestEntity mre = new MultipartRequestEntity(parts, postMethod.getParams());
postMethod.setRequestEntity(mre); HttpClient client = new HttpClient();
// 由于要上传的文件可能比较大 , 因此在此设置最大的连接超时时间
client.getHttpConnectionManager().getParams() .setConnectionTimeout(50000); int status = client.executeMethod(postMethod);
if (status == HttpStatus.SC_OK) {
InputStream inputStream = postMethod.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); StringBuffer stringBuffer = new StringBuffer();
String str = "";
while ((str = br.readLine()) != null) {
stringBuffer.append(str);
}
body = stringBuffer.toString();
} else {
body = "fail";
}
} catch (Exception e) {
logger.warn("上传文件异常", e);
} finally {
// 释放连接
postMethod.releaseConnection();
}
return body;
} /**
* 字节流读写复制文件
* @param src 源文件
* @param out 目标文件
*/
public static void InputStreamOutputStream(String src, String out) {
FileOutputStream outputStream = null;
FileInputStream inputStream = null;
try {
outputStream = new FileOutputStream(out);
inputStream = new FileInputStream(src);
byte[] bytes = new byte[1024];
int num = 0;
while ((num = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, num);
outputStream.flush();
} } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} } /** *//**文件重命名
* @param path 文件目录
* @param oldname 原来的文件名
* @param newname 新文件名
*/
public static void renameFile(String path,String oldname,String newname){
if(!oldname.equals(newname)){//新的文件名和以前文件名不同时,才有必要进行重命名
File oldfile=new File(path+"/"+oldname);
File newfile=new File(path+"/"+newname);
if(!oldfile.exists()){
return;//重命名文件不存在
}
if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同,则不允许重命名
System.out.println(newname+"已经存在!");
else{
oldfile.renameTo(newfile);
}
}else{
System.out.println("新文件名和旧文件名相同...");
}
} public static void main(String[] args) throws Exception {
String path = "C:/SCApp/Data/file/WU_FILE_2/";
String oldName = "410C0166CE6C5375D5990F8C3C486F01.mp4";
String md5 = DigestUtils.md5Hex(new FileInputStream(path + oldName));
String newName = md5 + "" +oldName.substring(oldName.lastIndexOf(".") );
renameFile(path,oldName,newName);
String body = fileload("https://resource.openedu.club/resUpload", new File(path + newName));
System.out.println(body);
}
}
package downloadTest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class down {
/**
* @功能 下载临时素材接口
* @param filePath 文件将要保存的目录
* @param method 请求方法,包括POST和GET
* @param url 请求的路径
* param fileName 带格式的文件名
* @return
*/ public static File saveUrlAs(String url,String filePath,String method,String fileName){
//System.out.println("fileName---->"+filePath);
//创建不同的文件夹目录
File file=new File(filePath);
//判断文件夹是否存在
if (!file.exists())
{
//如果文件夹不存在,则创建新的的文件夹
file.mkdirs();
}
FileOutputStream fileOut = null;
HttpURLConnection conn = null;
InputStream inputStream = null;
try
{
// 建立链接
URL httpUrl=new URL(url);
conn=(HttpURLConnection) httpUrl.openConnection();
//以Post方式提交表单,默认get方式
conn.setRequestMethod(method);
conn.setDoInput(true);
conn.setDoOutput(true);
// post方式不能使用缓存
conn.setUseCaches(false);
//连接指定的资源
conn.connect();
//获取网络输入流
inputStream=conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(inputStream);
//判断文件的保存路径后面是否以/结尾
if (!filePath.endsWith("/")) { filePath += "/"; }
//写入到文件(注意文件保存路径的后面一定要加上文件的名称)
fileOut = new FileOutputStream(filePath + fileName);
BufferedOutputStream bos = new BufferedOutputStream(fileOut); byte[] buf = new byte[4096];
int length = bis.read(buf);
//保存文件
while(length != -1)
{
bos.write(buf, 0, length);
length = bis.read(buf);
}
bos.close();
bis.close();
conn.disconnect();
} catch (Exception e)
{
e.printStackTrace();
System.out.println("抛出异常!!");
} return file; } /**
* @param args
*/
public static void main(String[] args)
{
String photoUrl = ".域名/ResourceData/PDF/2017/12/5/b79147d7936641a1b988a85b74d2a782.pdf";
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
System.out.println("fileName---->"+fileName);
String filePath = "C:\\SCApp";
File file = saveUrlAs(photoUrl, filePath , "GET", fileName);
System.out.println("Run ok!/n<BR>Get URL file " + file); } }

/**
* 此处部分自用,处理上传接收用
*/


package club.openedu.resource.controller;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver; import club.openedu.core.controller.BaseController;
import club.openedu.core.dao.OpenDao;
import club.openedu.resource.util.CapturePdfFirstPageUtil;
import club.openedu.resource.util.ConverterUtil;
import club.openedu.resource.util.ImageCutUtil;
import club.openedu.resource.util.VideoCoverMp4Util;
import club.openedu.resource.util.VideoCutUtil;
import club.openedu.resource.vo.MultipartFileParam;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @RestController
public class FileUploadController extends BaseController { @Resource
OpenDao openDap; public OpenDao getOpenDap() {
return openDap;
} public void setOpenDap(OpenDao openDap) {
this.openDap = openDap;
} @Value("${server.port}")
private int port; @Value("${web.serverIP}")
private String address; @Value("${web.upload-path}")
private String uploadPath; @Value("${syspath.ffmpeg-path}")
private String FFMPEG_PATH; private final static Logger logger= LoggerFactory.getLogger(FileUploadController.class); private static AtomicLong counter = new AtomicLong(0L); @RequestMapping("uploadfile")
public Object uploadv2(MultipartFileParam param) throws Exception {
//String uploadPath = request.getServletContext().getRealPath( "/" );
//uploadPath = uploadPath.replace( "\\", "/" ); Map<String, Object> map = getParameterMap();
String path =null; try { String prefix = "req_count:" + counter.incrementAndGet() + ":";
logger.info(prefix + "start !!!");
//使用 工具类解析相关参数,工具类代码见下面 logger.info("");
logger.info("");
logger.info(""); logger.info(prefix + "chunks= " + param.getChunks());
logger.info(prefix + "chunk= " + param.getChunk());
logger.info(prefix + "chunkSize= " + param.getSize()); //这个必须与前端设定的值一致
long chunkSize = 1024 * 1024; String finalDirPath = uploadPath + "file/";
logger.info(finalDirPath); String tempDirPath = finalDirPath + param.getId(); String tempFileName = param.getName(); File confFile = new File(tempDirPath, param.getName() + ".conf"); File tmpDir = new File(tempDirPath); File tmpFile = new File(tempDirPath, tempFileName); if (!tmpDir.exists()) {
tmpDir.mkdirs();
} RandomAccessFile accessTmpFile = new RandomAccessFile(tmpFile, "rw"); RandomAccessFile accessConfFile = new RandomAccessFile(confFile, "rw"); long offset = chunkSize * param.getChunk();
//定位到该分片的偏移量
accessTmpFile.seek(offset);
//写入该分片数据
accessTmpFile.write(param.getFile().getBytes()); //把该分段标记为 true 表示完成
logger.info(prefix + "set part " + param.getChunk() + " complete"); accessConfFile.setLength(param.getChunks());
accessConfFile.seek(param.getChunk());
accessConfFile.write(Byte.MAX_VALUE); //completeList 检查是否全部完成,如果数组里是否全部都是(全部分片都成功上传)
byte[] completeList = FileUtils.readFileToByteArray(confFile);
byte isComplete = Byte.MAX_VALUE;
for (int i = 0; i < completeList.length && isComplete==Byte.MAX_VALUE; i++) {
//与运算, 如果有部分没有完成则 isComplete 不是 Byte.MAX_VALUE
isComplete = (byte)(isComplete & completeList[i]); logger.info(prefix + "check part " + i + " complete?:" + completeList[i]);
} if (isComplete == Byte.MAX_VALUE) { logger.info(prefix + "upload complete !!" +
"---------------------------------------------------------");
renameFile(tempDirPath +"/"+tempFileName,tempDirPath+"/"+param.getName()); path ="/"+param.getId()+"/"+ param.getName(); } accessTmpFile.close();
accessConfFile.close(); logger.info(prefix + "end !!!"); }catch (Exception e){
e.printStackTrace();
} /**
* 文件上传完成,开始进行格式处理
*/
if (path!=null){ logger.info("path!=null ----" + uploadPath+"file"+path); //获取文件MD5
String md5 = DigestUtils.md5Hex(new FileInputStream(uploadPath+"file"+path)); //到数据库对比是否存在该文件
map.put("sqlMapId", "getFileIsThisMd5");
map.put("md5", md5);
List<Map<String, Object>> md5List = openDap.queryForList(map); //获取文件类型
String fileType = map.get("type").toString();
String inputFilePath = uploadPath+"file"+path;
String filePath = uploadPath + "file/" + param.getId();
String fileName = map.get("name").toString();
int pos = fileName.lastIndexOf("."); map.put("fileName", fileName.substring(0,pos));
map.put("fileExtName", fileName.substring(pos+1));
map.put("filePath", filePath);
map.put("fileSize", map.get("size").toString());
map.put("resPath", filePath);
map.put("sqlMapId", "saveFileInfo");
String resPk = openDap.insert(map).toString();
map.put("resPk", resPk); if(md5List.size() <= 0){ logger.info("文件不存在,开始处理..."); //office文件
if(fileType.contains("doc") || fileType.contains("docx") || fileType.contains("ppt") || fileType.contains("pptx") || fileType.contains("xls") || fileType.contains("xlsx") || fileType.contains("powerpoint")) {
//转换HTML、PDF、处理缩略图
ConverterUtil converter = new ConverterUtil(uploadPath+"file"+path);
converter.start(); map.put("pdfName", param.getName() + ".pdf");
map.put("htmlName", param.getName() + ".html");
map.put("imgName", param.getName() + ".jpg"); map.put("sqlMapId", "savePdfOffice");
openDap.insert(map); map.put("sqlMapId", "saveHtmlOffice");
openDap.insert(map); map.put("sqlMapId", "saveImgForFile");
openDap.insert(map);
}else if(fileType.contains("image") ) { //图片文件
//缩略图处理
ImageCutUtil imgUtil = new ImageCutUtil(inputFilePath, inputFilePath+".jpg", 150);
imgUtil.start(); map.put("imgName", param.getName() + ".jpg");
map.put("sqlMapId", "saveImgForFile");
openDap.insert(map);
}else if(fileType.contains("audio") ) { //音频
//设置默认缩略图
}else if(fileType.contains("pdf") ) { //pdf
//缩略图处理
String outputFilePath=inputFilePath+".jpg";
CapturePdfFirstPageUtil pdfUtil = new CapturePdfFirstPageUtil(ConverterUtil.getOutputFilePath(inputFilePath,".pdf"), outputFilePath);
pdfUtil.start(); map.put("imgName", param.getName() + ".jpg");
map.put("sqlMapId", "saveImgForFile");
openDap.insert(map);
}else if(fileType.contains("video") ) { //视频
logger.info("进入视频处理……");
//截取缩略图
VideoCutUtil videoUtil = new VideoCutUtil(inputFilePath, inputFilePath+".jpg", 2, "450x230",FFMPEG_PATH);
videoUtil.start(); map.put("imgName", param.getName() + ".jpg");
map.put("sqlMapId", "saveImgForFile");
openDap.insert(map); if(fileType.contains("mp4") ) {
logger.info("mp4文件,不做处理");
}else {
//转码
VideoCoverMp4Util converter = new VideoCoverMp4Util(uploadPath+"file",path,FFMPEG_PATH);
converter.start(); map.put("mp4Name", param.getName() + ".mp4");
map.put("sqlMapId", "saveMp4ForFile");
openDap.insert(map);
}
}else { //其他
logger.info("其他文件,不做处理");
}
}else {
logger.info("文件已存在");
} logger.info(uploadPath);
logger.info(path);
logger.info(uploadPath+"file"+path); String returnPath = address+":"+port+"/file"+path; return returnPath;
} return "还在上传中";
} private void renameFile(String file, String toFile){
File toBeRenamed = new File(file);
//检查要重命名的文件是否存在,是否是文件
if (!toBeRenamed.exists() || toBeRenamed.isDirectory()) { logger.info("File does not exist: " + file);
return;
} File newFile = new File(toFile); //修改文件名
if (toBeRenamed.renameTo(newFile)) {
logger.info("File has been renamed.");
} else {
logger.info("Error renmaing file");
} } @RequestMapping(consumes = "multipart/form-data", value = "/resUpload", method = RequestMethod.POST)
public String springUpload(HttpServletRequest request) throws IllegalStateException, IOException {
long startTime = System.currentTimeMillis();
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator iter = multiRequest.getFileNames(); String path = uploadPath + "file/WU_FILE_RES";
File pathFile = new File(path);
if(!pathFile.exists()) {
pathFile.mkdirs();
}
path = path + "/"; //上传文件
while (iter.hasNext()) {
MultipartFile file = multiRequest.getFile(iter.next().toString());
if (file != null) {
path = path + file.getOriginalFilename();
file.transferTo(new File(path));
} String filePath = path ;
String contentType = path.substring(path.lastIndexOf(".")+1);
logger.info("文件格式: " + contentType);
logger.info(contentType.equals("mp4")+"");
//office文件
if(contentType.contains("doc") || contentType.contains("docx") || contentType.contains("ppt") || contentType.contains("pptx") || contentType.contains("xls") || contentType.contains("xlsx") || contentType.contains("powerpoint")) {
//转换HTML、PDF、处理缩略图
ConverterUtil converter = new ConverterUtil(path);
converter.start(); }else if(contentType.contains("pdf") ) { //pdf
//缩略图处理 CapturePdfFirstPageUtil pdfUtil = new CapturePdfFirstPageUtil(ConverterUtil.getOutputFilePath(filePath,".pdf"), path+".jpg");
pdfUtil.start(); }else if(contentType.equals("mp4")) { //视频
logger.info("进入视频处理……"); String os = System.getProperty("os.name");
if(os.toLowerCase().startsWith("win")){
FFMPEG_PATH = "C:/Program Files/WorkSoftWare/ffmpeg/ffmpeg/bin/ffmpeg.exe";
}else {
FFMPEG_PATH = "/usr/local/ffmpeg/bin/ffmpeg";
}
logger.info("FFMPEG_PATH:" + FFMPEG_PATH); //截取缩略图
VideoCutUtil videoUtil = new VideoCutUtil(filePath, filePath+".jpg", 2, "450x230",FFMPEG_PATH);
videoUtil.start(); if(contentType.contains("mp4") ) {
logger.info("mp4文件,不做处理");
}else {
//转码
VideoCoverMp4Util converter = new VideoCoverMp4Util(uploadPath+"file",path,FFMPEG_PATH);
converter.start(); }
}else { //其他
logger.info("其他文件,不做处理");
}
} }
long endTime = System.currentTimeMillis();
System.out.println("资源中心文件自动上传处理时间:" + String.valueOf(endTime - startTime) + "ms");
return "/success";
} }

java前后台开发之后台自动上传下载的更多相关文章

  1. 不想加班开发管理后台了,试试这个 Java 开源项目吧!

    本文适合有 Java 基础并了解 SpringBoot 框架的同学 本文作者:HelloGitHub-嘉文 这里是 HelloGitHub 推出的<讲解开源项目>系列,今天给大家带来一款开 ...

  2. Java 集成开发环境的介绍及下载

    集成开发环境(integrated development environment,JDE) 之前成功运行了Java小程序是经历了先在笔记本中编写源代码,然后通过命令行运行打开javac编译源文件, ...

  3. 11-01 Java 开发工具 eclipse从下载、安装到实际使用的详细教程

     Eclipse和MyEclipse简介 Eclipse是一种可扩展的开放源代码的IDE.起始于1999年4月,由OTI和IBM两家公司的IDE产品开发组组建. 2001年11月,IBM公司捐出价值4 ...

  4. Java Web 开发填坑记- 如何正确的下载 Eclipse

    一直以来,做 Java web 开发都是用 eclipse , 可是到 eclipse 官网一看,我的天 http://www.eclipse.org/downloads/eclipse-packag ...

  5. ios开发视频播放后台下载功能实现 :1,ios播放视频 ,包含基于AVPlayer播放器,2,实现下载,iOS后台下载(多任务同时下载,单任务下载,下载进度,下载百分比,文件大小,下载状态)(真机调试功能正常)

    ABBPlayerKit ios开发视频播放后台下载功能实现 : 代码下载地址:https://github.com/niexiaobo/ABBPlayerKit github资料学习和下载地址:ht ...

  6. Java SE开发系列-JDK下载安装

    JDK下载安装 JDK是Java的开发环境,目前JDK内部也包含了JRE,JRE主要是JAVA程序的运行环境. 点击官方下载地址,按着下图操作即可下载对应系统的不同版本JDK. 进入页面滑到页面底部点 ...

  7. 慕课网金职位 Java工程师2020 百度网盘下载

    百度网盘链接:https://pan.baidu.com/s/1xshLRO3ru0LAsQQ0pE67Qg 提取码:bh9f 如果失效加我微信:610060008[视频不加密,资料代码齐全,超清一手 ...

  8. 用Spring Boot颠覆Java应用开发

    Java开发概述: 使用Java做Web应用开发已经有近20年的历史了,从最初的Servlet1.0一步步演化到现在如此多的框架,库以及整个生态系统.经过这么长时间的发展,Java作为一个成熟的语言, ...

  9. Java Web开发介绍

    转自:http://www.cnblogs.com/pythontesting/p/4963021.html Java Web开发介绍 简介 Java很好地支持web开发,在桌面上Eclipse RC ...

随机推荐

  1. 【原创】k8s源代码分析-----kubelet(1)主要流程

    本人空间链接http://user.qzone.qq.com/29185807/blog/1460015727 源代码为k8s v1.1.1稳定版本号 kubelet代码比較复杂.主要是由于其担负的任 ...

  2. wget镜像网站并且下载到指定目录 2012-06-20 19:40:56

    wget镜像网站并且下载到指定目录 2012-06-20 19:40:56 分类: Python/Ruby wget -r -p -np -k -P /tmp/ap http://www.exampl ...

  3. 虚拟机下安装centos7方法,修改系统语言为简体中文的方法

    说明 自己装系统时一般都可以自定义选择系统语言.可是云端服务器一般都是安装好的镜像,默认系统语言为英文,对于初学者可能还会有搞不懂的计算机词汇.这里简单说一下centos7怎么修改系统语言为中文. 虚 ...

  4. [转] C#与Java的比较

    [转] C#与Java的比较 转自:C#与Java的比较 2015-06-26 目录 一.C#.C++和Java二.语言规范的比较  2.1.简单数据类型  2.2.常量  2.3.公用类的入口点  ...

  5. django 判断用户是否登陆

    基于类的视图登陆

  6. maven 引入 net sf jsonlib 报错 has borken path

    pom.xml 内容: <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json ...

  7. python将str转换成字典

    典型的应用场景:Json数据的解析 >>> user "{'name' : 'jim', 'sex' : 'male', 'age': 18}" >> ...

  8. 一款基于jquery超炫的弹出层提示消息

    今天给大家带来一款基于jquery超炫的弹出层提示消息.这款实例页面初始时,一个go按钮.当单击go按钮时,提示强出层以动画形式出现.效果图如下: 在线预览   源码下载 实现的代码. html代码: ...

  9. Entity Framework中的实体类添加复合主键

    使用Code First模式实现给实体类添加复合主键,代码如下: using System; using System.Collections.Generic; using System.Compon ...

  10. Lua中的table函数库

    table.concat(table, sep,  start, end) concat是concatenate(连锁, 连接)的缩写. table.concat()函数列出参数中指定table的数组 ...