Spring文件上传
public void create(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
try
{
Process process = new Process();
List list = new ArrayList();
try
{
MultipartHttpServletRequest multipartRequest = null;
multipartRequest = (MultipartHttpServletRequest)request;
CommonsMultipartFile orginalFile = (CommonsMultipartFile)multipartRequest.getFile("file");
if (!(orginalFile.isEmpty())) {
String newFileName =
((String)getFileNameAndFileType(
orginalFile.getOriginalFilename()).get("fileName")) +
"_" +
new Date().getTime() +
"." +
((String)getFileNameAndFileType(orginalFile.getOriginalFilename()).get("fileType"));
String path = request.getSession().getServletContext().getRealPath("/") + "/data/version/";
String basePath = newFileName;
logger.info(basePath);
process.setUrl(basePath);
File file = new File(path);
if (!(file.exists())) {
file.mkdirs();
}
InputStream in = orginalFile.getInputStream();
OutputStream out = new FileOutputStream(path + newFileName);
int byteread = 0;
byte[] buffer = new byte[1048576];
while ((byteread = in.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, byteread);
}
in.close();
out.close();
BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream(path +
newFileName), "GBK"));
while (br.ready())
list.add(StringUtils.trimToEmpty(br.readLine()));
}
}
catch (Exception e) {
e.printStackTrace();
rendText(response, "{success:false,msg:'文件上传失败'}");
return;
}
this.processManager.saveProcess(process);
String contextPath = request.getSession().getServletContext().getRealPath("/");
contextPath = contextPath + "/view/terminal/version/version.txt";
this.processManager.exportData(contextPath);
responseMessage(response, "数据保存成功", true);
}
catch (BusinessException e)
{
setFailureMessage(request, e.getMessage());
responseMessage(response, e.getMessage(), false);
} catch (Exception e) {
setFailureMessage(request, e.getMessage());
responseMessage(response, "数据保存异常", false);
}
throws Exception
{
try
{
Process process = new Process();
List list = new ArrayList();
try
{
MultipartHttpServletRequest multipartRequest = null;
multipartRequest = (MultipartHttpServletRequest)request;
CommonsMultipartFile orginalFile = (CommonsMultipartFile)multipartRequest.getFile("file");
if (!(orginalFile.isEmpty())) {
String newFileName =
((String)getFileNameAndFileType(
orginalFile.getOriginalFilename()).get("fileName")) +
"_" +
new Date().getTime() +
"." +
((String)getFileNameAndFileType(orginalFile.getOriginalFilename()).get("fileType"));
String path = request.getSession().getServletContext().getRealPath("/") + "/data/version/";
String basePath = newFileName;
logger.info(basePath);
process.setUrl(basePath);
File file = new File(path);
if (!(file.exists())) {
file.mkdirs();
}
InputStream in = orginalFile.getInputStream();
OutputStream out = new FileOutputStream(path + newFileName);
int byteread = 0;
byte[] buffer = new byte[1048576];
while ((byteread = in.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, byteread);
}
in.close();
out.close();
BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream(path +
newFileName), "GBK"));
while (br.ready())
list.add(StringUtils.trimToEmpty(br.readLine()));
}
}
catch (Exception e) {
e.printStackTrace();
rendText(response, "{success:false,msg:'文件上传失败'}");
return;
}
this.processManager.saveProcess(process);
String contextPath = request.getSession().getServletContext().getRealPath("/");
contextPath = contextPath + "/view/terminal/version/version.txt";
this.processManager.exportData(contextPath);
responseMessage(response, "数据保存成功", true);
}
catch (BusinessException e)
{
setFailureMessage(request, e.getMessage());
responseMessage(response, e.getMessage(), false);
} catch (Exception e) {
setFailureMessage(request, e.getMessage());
responseMessage(response, "数据保存异常", false);
}
}
protected void rendText(HttpServletResponse response, String content)
throws IOException
{
response.setCharacterEncoding("GBK");
response.getWriter().write(content);
}
Map<String, String> getFileNameAndFileType(String fileName)
{
Map map = new HashMap();
String[] s = fileName.split("\\.");
map.put("fileName", s[0]);
map.put("fileType", s[1]);
return map;
}