大神们,帮帮忙吧

来源:3-1 [实现Java图片等比例缩略图] 应用程序框架搭建

qq_慕慕粉

2017-11-02 17:39

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">

<display-name>Z_Test</display-name>

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext-mvc.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>


<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

</web-app>

--------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xmlns:tx="http://www.springframework.org/schema/tx" 

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">


<mvc:default-servlet-handler/>

<mvc:annotation-driven/>

<context:component-scan base-package="*"/>

<bean id="viewResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="defaultEnconding " value="utf-8"></property>

<property name="maxUploadSize " value="10485760000"></property>

<property name="maxInMemorySize " value="40960"></property>


</bean>



</beans>

------------------------------------------------------------------------------------------

@Controller

@RequestMapping("/")

public class ThumbnailAction {

private UploadService uploadService;

private ThumbnailService thumbnailService;

@RequestMapping(value="/thumbnail",method=RequestMethod.POST)

public ModelAndView thumbnail(@RequestParam("image")CommonsMultipartFile file,HttpSession session)throws Exception{

//上传后图片的路径(相对路径)

String uploadPath = "/images";

//转化为在服务器的绝对路径

String realUploadPath=session.getServletContext().getRealPath(uploadPath);

//原图在服务器上的相对路径信息

String imageUrl=uploadService.uploadImage(file, uploadPath, realUploadPath);

//缩略图的访问路径

String thumImageUrl=thumbnailService.thumbnail(file, uploadPath, realUploadPath);

ModelAndView ret=new ModelAndView();

//设置返回的参数信息

ret.addObject("imageURl",imageUrl);

ret.addObject("thumImageURl",thumImageUrl);

//thumbnail:缩略图的名字

ret.setViewName("thumbnail");

return ret;

}

@Autowired

public void setUploadService(UploadService uploadService) {

this.uploadService = uploadService;

}

@Autowired

public void setThumbnailService(ThumbnailService thumbnailService) {

this.thumbnailService = thumbnailService;

}

---------------------------------------------------------------------------------------------

@Service

public class ThumbnailService {

public static final int WIDTH = 100;

public static final int HEIGHT = 100;

public String thumbnail(CommonsMultipartFile file, String uploadPath, String realUploadPath ){

try {

String des = realUploadPath+"/thum"+file.getOriginalFilename();

//通过原图的输入流,获得原图的数据信息,指定大小;toFile(des):保存到服务器上

Thumbnails.of(file.getInputStream()).size(WIDTH, HEIGHT).toFile(des);

} catch (Exception e) {

e.printStackTrace();

}

//

return uploadPath + "/thum_" + file.getOriginalFilename();

}

-------------------------------------------------------------------------------------

@Service

public class UploadService {


public String uploadImage(CommonsMultipartFile file, String uploadPath, String realUploadPath) {

InputStream is = null;

OutputStream os = null;


try {

//获取输入流信息

is=file.getInputStream();

//目标路径=服务器的绝对路径+文件名称

String des = realUploadPath+"/"+file.getOriginalFilename();

//输出流 指向 目标文件的路径

os=new FileOutputStream(des);

byte[] buffer=new byte[1024];

int len = 0;

//通过输入流读取字节,字节大小大于0,说明有内容

while ((len=is.read(buffer))>0) {

//输出流循环写出来

os.write(buffer);

}


} catch (Exception e) {

e.printStackTrace();

} finally {

if (is != null) {

try {

is.close();

} catch (Exception e) {

e.printStackTrace();

}

}

if (os != null) {

try {

os.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}


// 原图访问路径

return uploadPath + "/" + file.getOriginalFilename();


}


写回答 关注

2回答

  • 叶0528
    2018-10-19 09:39:45
    是不是要:如果目录不存在,创建
    /**
     * 上传文件
     * @param file 文件
     * @param uploadPath 上传相对路径
     * @param realUploadPath 绝对路径
     * @return
     */
    public  String uploadImage(MultipartFile file, String uploadPath, String realUploadPath){
        InputStream is = null;
        OutputStream os = null;
         
         //如果目录不存在,创建
    
    
        try {
            is = file.getInputStream();


  • 大叔_fighting
    2017-12-07 16:53:45

    ?????你要问啥????

Java实现图片等比例缩略图

视频教程以实例的方式,讲解Java平台实现图片等比例缩略图生成

25335 学习 · 16 问题

查看课程

相似问题