文件的上傳
首選導(dǎo)入與文件上傳相關(guān)jar包
form表單中要添加
enctype="multipart/form-data"
用post請(qǐng)求提交
表單代碼如下:
<form action="<%=path%>/fff/aaa.do" method="post" enctype="multipart/form-data">
1.<input name="u1"/><br/>
2.<input name="a1"/><br/><br/>
3.<input type="file" name="mf"/><br/>
4.<input type="file" name="mf"/><br/><br/>
<input type="submit"/>
</form>
對(duì)應(yīng)方法中參數(shù)為MultipartFile[] f
參數(shù)前面可以加上注解@RequestParam(name="mf",required=false)
表示用戶可以不上傳文件程序也不會(huì)報(bào)錯(cuò)
參數(shù)中也要有HttpServletRequest來獲得項(xiàng)目路徑
1.在WebRoot下新建一個(gè)imgs/kind的文件夾來存放上傳的圖片
2.得到項(xiàng)目路徑+"/imgs/kind/"
req.getSession().getServletContext().getRealPath("")+"/imgs/kind/";
3.判斷f是否為空,遍歷f這個(gè)數(shù)組
4.判斷上傳的每個(gè)文件大小是否>0
5.是的話
File iof = new File('圖片存放路徑'+f[i].getOriginalFilename());
6.把Spring的MultipartFile保存成java.io.File
f[i].transferTo(iof);
完整代碼如下:
@RequestMapping("/aaa")
public String f1(String u1,String a1,@RequestParam(name="mf",required=false) MultipartFile[] f,HttpServletRequest req) throws IOException{
//解析后 獲取 表單數(shù)據(jù)
System.out.println(this.getClass()+"日志1...u1="+u1+"\ta1="+a1+"\t上傳:"+(null!=f?f.length:-1));
String directory = req.getSession().getServletContext().getRealPath("")+"/imgs/kind/";
//保存
if(null != f && f.length>0){
for (int i = 0; i < f.length; i++) {
if( f[i].getSize() > 0){
System.out.println("日志2...."+f[i].getOriginalFilename());
File iof = new File(directory+f[i].getOriginalFilename());
f[i].transferTo(iof); //spring的MultipartFile 保存成 java.io.File
}
}
}
return "test2";
}//f1()
文件的下載
返回值要為ResponseEntity<byte[]>
參數(shù)為HttpServletRequest也是用來獲得項(xiàng)目路徑
步驟:
1.得到下載的文件路徑
//從數(shù)據(jù)庫查詢出對(duì)象 的資源名稱 蠢熄,假如是個(gè)圖片
String fileName="jia.png";
String directory = req.getSession().getServletContext().getRealPath("")+"/imgs/kind/" + fileName;
2.設(shè)置頭信息
//設(shè)置下載頭信息
HttpHeaders hders = new HttpHeaders();
hders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
hders.setContentDispositionFormData("attchment", fileName);
3.讀取文件變成字節(jié)數(shù)組
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File(directory)), hders, HttpStatus.CREATED) ;
國際化
寫語言的配置文件
abc_en.properties
abc_ja.properties
abc_zh.properties
文件放在src下,鍵一樣值寫自己對(duì)應(yīng)的語言
跳轉(zhuǎn)鏈接
?后加上locale=代表的語言
<a href="<%=path%>/fff/bbb.do?locale=zh">中文</a>
<a href="<%=path%>/fff/bbb.do?locale=en">english</a>
<a href="<%=path%>/fff/bbb.do?locale=ja">さつ</a>
對(duì)應(yīng)的方法跳到test2.jsp頁面
@RequestMapping("/bbb")
public String f2(){
System.out.println(this.getClass() + "執(zhí)行... f2()");
return "test2";
}
頁面上要加
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
配置
abc_en.properties等配置文件中的語言調(diào)用通過<spring:messagecode="鍵" />
<h1><spring:message code="a.a.c.d" >
<spring:argument value="5"/>
</spring:message>
</h1>
<br/>
<spring:message code="k.ind.info" />
<br/>
<spring:message code="a.b.c.u1" />:<input name="u1"/><br/>
<spring:message code="a.b.c.u2" />:<input name="u2"/><br/><br/>
<input type="submit" value="<spring:message code="k.ind.btn"/>"/><br/>
配置
在spring-mvc.xml中配置
還要配置綁定國際化資源文件前綴
國際化解析器
國際化攔截器
整個(gè)配置文件代碼:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.senchen.controller"/>
<!-- 試圖解析器 springMVC管理的jsp文件位置應(yīng)該在 /WEB-INF/meto/ -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/meto/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 綁定國際化資源文件前綴 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="abc"/>
<property name="useCodeAsDefaultMessage" value="true"/>
</bean>
<!-- 國際化解析器 -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
<!-- 國際化攔截器 -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
</mvc:interceptors>
</beans>
自定義類型轉(zhuǎn)化器
繼承PropertyEditorSupport類
代碼如下:
package com.senchen.controller.util;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class YourConvert extends PropertyEditorSupport {
//默認(rèn)支持 2010-10-10
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
@Override
public void setAsText(String text) throws IllegalArgumentException {
// text 接收到的 要轉(zhuǎn)換的 字符串
System.out.println("YourConvert : 現(xiàn)在要轉(zhuǎn)換 " + text );
Date ret = null;
try {
ret = fmt.parse( text );
} catch (ParseException e) {
fmt = new SimpleDateFormat("yyyy年MM月dd日"); //否則嘗試 2010年10月10日
try {
ret = fmt.parse( text );
} catch (ParseException e1) {
fmt = new SimpleDateFormat("yyyy/MM/dd"); //否則嘗試 2010/10/10
try {
ret = fmt.parse( text );
} catch (ParseException e2) {
System.out.println("以上格式都不支持");
}
}
}
super.setValue( ret );
}//setAsText()
}
加載要使用的轉(zhuǎn)換器,在該方法上加注解:@InitBinder
Date.class表示碰見此類型就使用該轉(zhuǎn)化器
代碼如下:
@InitBinder
public void f1( WebDataBinder bind ){
System.out.println("注冊(cè)類型轉(zhuǎn)換器");
//注冊(cè)要使用的轉(zhuǎn)換器
bind.registerCustomEditor(Date.class, new YourConvert());
}
當(dāng)有方法中有Date類型時(shí)會(huì)自動(dòng)轉(zhuǎn)換
@RequestMapping("/aaa")
public String f1(String u1 ,Date u2){
System.out.println(this.getClass()+"日志1...u1:"+u1+"\t u2:" + u2);
return "test2";
}