天天看點

SpringMVC 檔案上傳下載下傳

多檔案上傳

public static List<String> imageUpload(String filename, HttpServletRequest request, HttpServletResponse response) throws Exception {

CommonsMultipartResolver cmr = new CommonsMultipartResolver(request.getServletContext());

// 上文圖檔路徑

List<String> listFile = new ArrayList<>();

String imageUrl = null;

if (cmr.isMultipart(request)) {

MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) (request);

List<MultipartFile> listImage = mRequest.getFiles(filename);

if (listImage != null && listImage.size() > 0) {

for (MultipartFile mFile : listImage) {

if (!mFile.isEmpty()) {

String suffix = mFile.getOriginalFilename().substring(mFile.getOriginalFilename().lastIndexOf("."));

suffix = suffix.toLowerCase();

if (suffix.equals(".jpg") || suffix.equals(".jpeg") || suffix.equals(".png")) {

String fileName = UUID.randomUUID().toString();

if (mFile.getSize() < 10000000 && mFile.getSize() > 0) {

PropertiesUtils pro = new PropertiesUtils();

// String path = pro.load("file.properties") +

// "/upload/image";

// 注意 注意 線上擷取上傳路徑位址

String path = request.getServletContext().getRealPath("upload/image/");

File dir = new File(path);

if (!dir.exists()) {

dir.mkdirs();

}

// 縮略圖上傳路徑 start

String desPath = path + "/thum_" + fileName;

// 壓縮圖檔

Thumbnails.of(mFile.getInputStream()).scale(1f).outputQuality(0.38f).toFile(desPath);

imageUrl = request.getRequestURL().toString();

int index = imageUrl.indexOf("xdfstar") + 7;

String url = imageUrl.substring(0, index);

imageUrl = url + "/upload/image/" + "thum_" + fileName + ".JPEG";

request.setAttribute("imageUrl", imageUrl);

// 存放所有上傳圖檔的路徑

listFile.add(imageUrl);

} else {

listFile = null;

return listFile;

imgurls = FileUpload.imageUpload("picture", request, response);

下載下傳:

public static void download(HttpServletRequest request,

HttpServletResponse response, String storeName, String contentType,

String realName) throws Exception {

response.setContentType("text/html;charset=UTF-8");

request.setCharacterEncoding("UTF-8");

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

String ctxPath = request.getSession().getServletContext()

.getRealPath("/")+"upload/image/";

String downLoadPath = ctxPath + storeName;

long fileLength = new File(downLoadPath).length();

response.setContentType(contentType);

response.setHeader("Content-disposition", "attachment; filename="

+ new String(realName.getBytes("utf-8"), "ISO8859-1"));

response.setHeader("Content-Length", String.valueOf(fileLength));

bis = new BufferedInputStream(new FileInputStream(downLoadPath));

bos = new BufferedOutputStream(response.getOutputStream());

byte[] buff = new byte[2048];

int bytesRead;

while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {

bos.write(buff, 0, bytesRead);

bis.close();

bos.close();

String contentType = "application/octet-stream";

try {

FileDownload.download(request, response, url, contentType,String.valueOf(new Random().nextInt(10000)+1)+".jpg");

catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

繼續閱讀