SpringBoot 学习记录(五)- 文件上传(和图片服务器fastdfs测试)

application.yml  配置文件代码:

spring:
  mvc:
    view:
      prefix: /WEB-INF/view/
      suffix: .jsp
fdfs:
  soTimeout: 1501
  connectTimeout: 601 
  thumbImage:             #缩略图生成参数
    width: 150
    height: 150
  trackerList:            #TrackerList参数,支持多个
    - 192.168.133.128:22122


Controller代码:


package com.phpfzh.web;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;


import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.DefaultGenerateStorageClient;


@Controller
public class DownloadControllerTest {

@Autowired
DefaultGenerateStorageClient defaultGenerateStorageClient; 

@RequestMapping(value = "/upload1",method = RequestMethod.POST)
public String upload(@RequestParam("file") MultipartFile file,HttpServletRequest request,HttpServletResponse response){
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);
try {
StorePath storePath =  defaultGenerateStorageClient.uploadFile("group1", file.getInputStream(), file.getSize(), suffixName);
System.out.println(storePath);
  } catch (IOException e) {
  e.printStackTrace();
}


return null;
}

}


test.jsp 代码:


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <a href="http://localhost:8080/download1">下載png文件</a>
  <a href="http://localhost:8080/download2">下載tar1文件</a>
 <a href="http://images.chenjiaming.com/group1/M00/00/00/rBKphlnDFduAf-RkAFLMV62e0mQ027.rar">下載tar文件</a>
 <form action="http://localhost:8080/upload1" method="post" enctype="multipart/form-data">
  <input type="file" name="file" value="文件上传"/>
  <input type="submit" value="提交">
 </form>
 
  
 </body>
</html>


启动类application 代码:

package com.phpfzh;


import javax.servlet.MultipartConfigElement;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;


@SpringBootApplication
public class Application {

         //这里配置文件大小限制
@Bean 
public MultipartConfigElement multipartConfigElement(){ 
MultipartConfigFactory config = new MultipartConfigFactory(); 
config.setMaxFileSize("80MB"); 
config.setMaxRequestSize("100MB"); 
return config.createMultipartConfig(); 


public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}



pom.xml 代码:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.phpfzh2</groupId>
<artifactId>spring-boot01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.20</version>
</dependency>


<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>


<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.25.4-RELEASE</version>
</dependency>

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
 
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <scope>provided</scope>
  </dependency>


<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <scope>provided</scope>
  </dependency>
 
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值