zoukankan      html  css  js  c++  java
  • JAVA 上传文件到本地服务器

    import java.io.*;
    
    
    public class FileUpload {
    
        /**
         *
         * @param file 文件流
         * @param destFilePath 保存到本地的目录路径
         * @return
         */
        public static String upload(MultipartFile file, String destFilePath) {
            InputStream inputStream = null;
            FileOutputStream fileOutputStream = null;
            try {
                String fileName = file.getOriginalFilename();
                String[] split = fileName.split("\\.");
                inputStream = file.getInputStream();
                // 数据缓冲
                byte[] buffer = new byte[1024 * 1024];
                //读取到的数据长度
                int len;
                // 输出的文件流保存到本地文件
                File tempFile = new File(destFilePath);
                if (!tempFile.exists()) {
                    tempFile.mkdirs();
                }
                //重新命名
                fileName = Math.random() +"."+ split[split.length-1];
                fileOutputStream = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
                // 开始读取
                while ((len = inputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, len);
                }
                return fileName;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 完毕,关闭所有链接
                try {
                    fileOutputStream.close();
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return "";
        }
    }
  • 相关阅读:
    ||和&&
    用jQuery编的一个分页小代码
    Intent携带额外的数据的方法
    Handler消息传递机制
    安卓中的消息提示
    使用AlertDialog创建对话框的大致步骤
    布局管理器
    Android中支持的常用距离单位
    开发自定义View
    Gridview中奇偶数行颜色设置
  • 原文地址:https://www.cnblogs.com/guliang/p/15635056.html
Copyright © 2011-2022 走看看