日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java mvc 导入_Java SpringMVC文件导入和导出

發布時間:2023/12/31 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java mvc 导入_Java SpringMVC文件导入和导出 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

J示例代碼:

@ResponseBody

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

public void uploadFile(HttpServletRequest request, HttpServletResponse response) throws Exception {

Map map = new HashMap();

PrintWriter writer = null;

String orgfilename = "";

String dataRandomNum = request.getParameter("dataRandomNum");

String filePath = "";

String path = Toolkit.getConfigValue("bd.sys.config.lzzb.filepath","C:\\Users\\Administrator\\AppData\\Local\\Temp\\HeiFei");

filePath = path+"\\"+dataRandomNum;

MultipartHttpServletRequest mhsr = (MultipartHttpServletRequest) request;

//(根據前臺的name名稱得到上傳的文件)

Iterator isFiles = mhsr.getFileNames();

while (isFiles.hasNext()) {

MultipartFile file = mhsr.getFile(isFiles.next());

String fileNames = file.getOriginalFilename();

orgfilename = fileNames;

File headPath = new File(filePath);// 獲取文件夾路徑

if (!headPath.exists()) {// 判斷文件夾是否創建,沒有創建則創建新文件夾

headPath.mkdirs();

}

OutputStream out = new FileOutputStream(new File(filePath, fileNames));

InputStream in = file.getInputStream();

int length = 0;

byte[] buf = new byte[1024];

while ((length = in.read(buf)) != -1) {

out.write(buf, 0, length);

}

in.close();

out.close();

/** 將上傳處理后的數據返回 **/

map.put("orgfilename", orgfilename);

map.put("filepath", (filePath + "\\" + orgfilename));

map.put("success", 200);

map.put("code", "200");

}

response.setContentType("text/xml; charset=UTF-8");

response.setHeader("Cache-Control", "no-cache");

response.setHeader("Pragma", "no-cache");

writer = response.getWriter();

String msg = JSON.toJSONString(map);

Boolean returnB = cyanobacteriaWeeklyService.insertCyanobacteriaWeeklyData(filePath);

if(returnB) {

map.put("data", "插入成功!");

}else {

map.put("data", "插入失敗!");

}

writer.print(msg);

}

@ResponseBody

@RequestMapping(value = "/exportFile")

public void exportFile(HttpServletRequest request, HttpServletResponse response) {

String startTime = request.getParameter("startTime");

String endTime = request.getParameter("endTime");

//省略數據處理

//獲取運行環境路徑

java.net.URL uri = this.getClass().getResource("/");

String filePash = uri.getPath();

filePash = filePash.substring(1, filePash.indexOf("WEB-INF"))+"resources/file/藍藻藻情報告_20200708.docx";

//render

XWPFTemplate template = XWPFTemplate.compile(filePash).render(dataMap);

OutputStream out = null;

try {

String fileName = year+"年"+month+"月巢湖藍藻藻情報告.docx";

//1、設置response 響應頭

response.reset();

response.setCharacterEncoding("UTF-8");

response.setContentType("application/octet-stream");

response.setHeader("Content-Disposition","attachment;fileName="+URLEncoder.encode(fileName, "UTF-8"));

//3、 寫出文件--輸出流

out = response.getOutputStream();

template.write(out);

template.close();

out.close();

} catch (FileNotFoundException e) {

LOGGER.error(e.getMessage());

} catch (IOException e) {

LOGGER.error(e.getMessage());

}finally {

try {

if(template != null) {

template.close();

}

} catch (IOException e) {

LOGGER.error(e.getMessage());

}

try {

if(out != null) {

out.close();

}

} catch (IOException e) {

LOGGER.error(e.getMessage());

}

}

}

包含:獲取Class數據路徑,文件導入和導出

總結

以上是生活随笔為你收集整理的java mvc 导入_Java SpringMVC文件导入和导出的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。