zoukankan      html  css  js  c++  java
  • java前后台开发之后台自动上传下载

    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"; } }
  • 相关阅读:
    vim复制
    嵌入式Linux学习(二)
    (Java实现) 洛谷 P1042 乒乓球
    (Java实现) 洛谷 P1042 乒乓球
    (Java实现) 洛谷 P1071 潜伏者
    (Java实现) 洛谷 P1071 潜伏者
    (Java实现) 洛谷 P1025 数的划分
    (Java实现)洛谷 P1093 奖学金
    (Java实现)洛谷 P1093 奖学金
    Java实现 洛谷 P1064 金明的预算方案
  • 原文地址:https://www.cnblogs.com/remember-forget/p/10087458.html
Copyright © 2011-2022 走看看