国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java實現(xiàn)文件保存到本地的方法

java實現(xiàn)文件保存到本地的方法

2020-08-02 11:59園芳寶貝 Java教程

本篇文章主要介紹了java實現(xiàn)文件保存到本地的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本篇介紹了java實現(xiàn)文件保存本地的方法,具體代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private void savePic(InputStream inputStream, String fileName) {
 
    OutputStream os = null;
    try {
      String path = "D:\\testFile\\";
      // 2、保存到臨時文件
      // 1K的數(shù)據(jù)緩沖
      byte[] bs = new byte[1024];
      // 讀取到的數(shù)據(jù)長度
      int len;
      // 輸出的文件流保存到本地文件
 
      File tempFile = new File(path);
      if (!tempFile.exists()) {
        tempFile.mkdirs();
      }
      os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
      // 開始讀取
      while ((len = inputStream.read(bs)) != -1) {
        os.write(bs, 0, len);
      }
 
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // 完畢,關(guān)閉所有鏈接
      try {
        os.close();
        inputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

文件保存方法.

附:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.ebways.web.upload.controller;
 
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import com.ebways.web.base.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ebways.web.upload.url.UploadURL;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@Controller
@RequestMapping(value = UploadURL.MODE_NAME)
public class UploadController extends BaseController {
 
  @RequestMapping(value = UploadURL.IMAGE_UPLOAD)
  @ResponseBody
  public String uploadFile(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IllegalArgumentException, Exception {
    // 參數(shù)列表
    Map<String, Object> map = new HashMap<>();
    map.put("file", file);
    savePic(file.getInputStream(), file.getOriginalFilename());
 
    //請求接口
    String apiReturnStr = "";//apiHttpRequest(map, API_HOST_URL + "/image/upload");
 
    return apiReturnStr;
  }
 
  private void savePic(InputStream inputStream, String fileName) {
 
    OutputStream os = null;
    try {
      String path = "D:\\testFile\\";
      // 2、保存到臨時文件
      // 1K的數(shù)據(jù)緩沖
      byte[] bs = new byte[1024];
      // 讀取到的數(shù)據(jù)長度
      int len;
      // 輸出的文件流保存到本地文件
 
      File tempFile = new File(path);
      if (!tempFile.exists()) {
        tempFile.mkdirs();
      }
      os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
      // 開始讀取
      while ((len = inputStream.read(bs)) != -1) {
        os.write(bs, 0, len);
      }
 
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // 完畢,關(guān)閉所有鏈接
      try {
        os.close();
        inputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
 
  /**
   * <p class="detail">
   * 功能:公共Action
   * </p>
   *
   * @date 2016年9月8日
   * @author wangsheng
   */
  private String allowSuffix = "jpg,png,gif,jpeg";//允許文件格式
  private long allowSize = 2L;//允許文件大小
  private String fileName;
  private String[] fileNames;
 
  public String getAllowSuffix() {
    return allowSuffix;
  }
 
  public void setAllowSuffix(String allowSuffix) {
    this.allowSuffix = allowSuffix;
  }
 
  public long getAllowSize() {
    return allowSize * 1024 * 1024;
  }
 
  public void setAllowSize(long allowSize) {
    this.allowSize = allowSize;
  }
 
  public String getFileName() {
    return fileName;
  }
 
  public void setFileName(String fileName) {
    this.fileName = fileName;
  }
 
  public String[] getFileNames() {
    return fileNames;
  }
 
  public void setFileNames(String[] fileNames) {
    this.fileNames = fileNames;
  }
 
 
  /**
   * <p class="detail">
   * 功能:重新命名文件
   * </p>
   *
   * @return
   * @author wangsheng
   * @date 2016年9月8日
   */
  private String getFileNameNew() {
    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    return fmt.format(new Date());
  }
 
  /**
   * <p class="detail">
   * 功能:文件上傳
   * </p>
   *
   * @param files
   * @param destDir
   * @throws Exception
   * @author wangsheng
   * @date 2014年9月25日
   */
  public void uploads(MultipartFile[] files, String destDir, HttpServletRequest request) throws Exception {
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
    try {
      fileNames = new String[files.length];
      int index = 0;
      for (MultipartFile file : files) {
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        int length = getAllowSuffix().indexOf(suffix);
        if (length == -1) {
          throw new Exception("請上傳允許格式的文件");
        }
        if (file.getSize() > getAllowSize()) {
          throw new Exception("您上傳的文件大小已經(jīng)超出范圍");
        }
        String realPath = request.getSession().getServletContext().getRealPath("/");
        File destFile = new File(realPath + destDir);
        if (!destFile.exists()) {
          destFile.mkdirs();
        }
        String fileNameNew = getFileNameNew() + "." + suffix;//
        File f = new File(destFile.getAbsoluteFile() + "\\" + fileNameNew);
        file.transferTo(f);
        f.createNewFile();
        fileNames[index++] = basePath + destDir + fileNameNew;
      }
    } catch (Exception e) {
      throw e;
    }
  }
 
  /**
   *功能:文件上傳
   *
   * @param file
   * @param destDir
   * @throws Exception
   * @author wangsheng
   * @date 2016年9月8日
   */
  public void upload(MultipartFile file, String destDir, HttpServletRequest request) throws Exception {
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
    try {
      String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
      int length = getAllowSuffix().indexOf(suffix);
      if (length == -1) {
        throw new Exception("請上傳允許格式的文件");
      }
      if (file.getSize() > getAllowSize()) {
        throw new Exception("您上傳的文件大小已經(jīng)超出范圍");
      }
 
      String realPath = request.getSession().getServletContext().getRealPath("/");
      File destFile = new File(realPath + destDir);
      if (!destFile.exists()) {
        destFile.mkdirs();
      }
      String fileNameNew = getFileNameNew() + "." + suffix;
      File f = new File(destFile.getAbsoluteFile() + "/" + fileNameNew);
      file.transferTo(f);
      f.createNewFile();
      fileName = basePath + destDir + fileNameNew;
    } catch (Exception e) {
      throw e;
    }
  }
 
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/gmq-sh/p/5852096.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 婷婷毛片 | 国产高清视频在线 | av成人在线观看 | 国产精品永久 | www.亚洲黄色 | 亚洲精品无 | 狠狠av| 亚洲国产中文字幕 | 99re热精品视频 | 一区二区乱码 | 欧美一区二区在线视频 | 超碰伊人网 | 狠狠视频| 日本在线视频一区二区 | 亚洲高清在线视频 | 国产精品毛片在线 | 国产精品久久久久久亚洲调教 | 看一级黄色大片 | 色综合社区 | 日本二区视频 | 国产精品二区三区 | 日韩在线精品强乱中文字幕 | 91视频免费网站 | 91精品国产高清一区二区三区 | 亚洲精品久久久久久久久久久久久 | 欧美一级视频在线观看 | 欧美大片黄 | 日韩三区 | 久久69精品久久久久久久电影好 | 亚洲国产精品一区二区三区 | 高清在线一区二区 | 91精品久久久久久综合五月天 | 天天干天天插 | 国产亚洲精品久久久闺蜜 | 狠狠干av | 国产日韩中文字幕 | 青娱乐国产精品视频 | 精品国产青草久久久久福利 | 欧美一区2区 | 成人免费观看视频 | 日韩午夜在线视频 |