最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java Spring MVC 上傳下載文件配置及controller方法詳解

 更新時(shí)間:2016年09月30日 16:47:29   作者:回魔一笑  
這篇文章主要介紹了Java Spring MVC 上傳下載文件配置及controller方法詳解,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下

下載:

1.在spring-mvc中配置(用于100M以下的文件下載)

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
<property name="messageConverters"> 
<list> 
<!--配置下載返回類型-->
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> 
<bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
<!--配置編碼方式-->
<property name="supportedMediaTypes" value="application/json; charset=UTF-8" /> 
</bean> 
</list> 
</property> 
</bean>

下載文件代碼

@RequestMapping("/file/{name.rp}")
public ResponseEntity<byte[]> fileDownLoad(@PathVariable("name.rp")String name, HttpServletRequest request,HttpServletResponse response) {
// @PathVariable String name,
// @RequestParam("name")String name,
// System.out.println("<name>"+name);
// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
ResponseEntity<byte[]> re = null;
try {
/**
* css,js,json,gif,png,bmp,jpg,ico,doc,docx,xls,xlsx,txt,swf,pdf
* **/
//下載防止靜態(tài)加載干擾
Feelutile f=new Feelutile();
name=f.getfileformat(name);
String pathString="C:\\tempDirectory\\"+name; 
File file=new File(pathString);
HttpHeaders headers=new HttpHeaders();
//String filename=URLEncoder.encode(name, "UTF-8");//為了解決中文名稱亂碼問題 
String filename=new String(name.getBytes("utf-8"),"utf-8");
byte[] by=FileUtils.readFileToByteArray(file);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//URLEncoder.encode(filename, "UTF-8")
headers.setContentDispositionFormData("attachment",filename);
headers.setContentLength(by.length);
re=new ResponseEntity<byte[]>(by, headers, HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace();
try {
request.getRequestDispatcher("/error/404.jsp").forward(request, response);
} catch (ServletException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return re;
}

上傳文件:

1在spring-mvc中配置

<!--4.文件上傳 配置 file upload -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<value>1048576000</value>
</property>
<property name="maxInMemorySize">
<value>40960</value>
</property>
</bean>

在controller中代碼如下

@RequestMapping(value="/upload", method = RequestMethod.POST)
@ResponseBody
public Json upload(Doc doc, @RequestParam("uploadFile") CommonsMultipartFile file) {
Json j = new Json();
try {
String realpath = this.servletContext.getRealPath("/upload"); 
String uploadFileFileName = file.getOriginalFilename(); 
String uploadFileFileNameWithoutSpace = uploadFileFileName.replaceAll(" ", ""); 
String fileType = uploadFileFileNameWithoutSpace.substring(uploadFileFileNameWithoutSpace.lastIndexOf("."));
File targetFile = new File(realpath+File.separator, uploadFileFileNameWithoutSpace);
if (targetFile.exists()) {
targetFile.delete();
}
file.getFileItem().write(targetFile); 
docService.upload(doc,uploadFileFileNameWithoutSpace);
j.setSuccess(true);
j.setMsg("Upload manual successfully");
}catch (Exception e) {
logger.error(ExceptionUtil.getExceptionMessage(e));
j.setMsg("Upload manual unsuccessfully");
}
return j;
}

以上所述是小編給大家介紹的Java Spring MVC 上傳下載文件配置及controller方法詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

合山市| 祥云县| 交口县| 张家界市| 中山市| 八宿县| 会昌县| 周至县| 杭州市| 集贤县| 望城县| 贺州市| 游戏| 滁州市| 武定县| 镇沅| 莲花县| 于都县| 新闻| 沅江市| 绵竹市| 沁源县| 嘉善县| 涪陵区| 哈尔滨市| 商丘市| 胶南市| 铁力市| 高安市| 察雅县| 星座| 历史| 永丰县| 江都市| 玉门市| 泽普县| 泸西县| 高安市| 二手房| 安义县| 从江县|