程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java Spring MVC 上傳下載文件設置裝備擺設及controller辦法詳解

Java Spring MVC 上傳下載文件設置裝備擺設及controller辦法詳解

編輯:關於JAVA

Java Spring MVC 上傳下載文件設置裝備擺設及controller辦法詳解。本站提示廣大學習愛好者:(Java Spring MVC 上傳下載文件設置裝備擺設及controller辦法詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Java Spring MVC 上傳下載文件設置裝備擺設及controller辦法詳解正文


下載:

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
* **/
//下載避免靜態加載攪擾
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辦法詳解,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved