大神們,幫幫忙吧
<?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";
//轉(zhuǎn)化為在服務(wù)器的絕對路徑
String realUploadPath=session.getServletContext().getRealPath(uploadPath);
//原圖在服務(wù)器上的相對路徑信息
String imageUrl=uploadService.uploadImage(file, uploadPath, realUploadPath);
//縮略圖的訪問路徑
String thumImageUrl=thumbnailService.thumbnail(file, uploadPath, realUploadPath);
ModelAndView ret=new ModelAndView();
//設(shè)置返回的參數(shù)信息
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();
//通過原圖的輸入流,獲得原圖的數(shù)據(jù)信息,指定大小;toFile(des):保存到服務(wù)器上
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();
//目標路徑=服務(wù)器的絕對路徑+文件名稱
String des = realUploadPath+"/"+file.getOriginalFilename();
//輸出流 指向 目標文件的路徑
os=new FileOutputStream(des);
byte[] buffer=new byte[1024];
int len = 0;
//通過輸入流讀取字節(jié),字節(jié)大小大于0,說明有內(nèi)容
while ((len=is.read(buffer))>0) {
//輸出流循環(huán)寫出來
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();
}
2018-10-19
2017-12-07
?????你要問啥????