import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
/**
* 上传文档
* @param upExcel
* @return
*/
@RequestMapping(value="/formExcel",method = RequestMethod.POST)
@ResponseBody
public void formExcel(MultipartFile upExcel,String fileName ){
System.out.println(upExcel.getOriginalFilename()+"----"+fileName);
try {
String originalFilename = fileName;
if(originalFilename==null||originalFilename==""){
// return "请选择要上传的文件";
// this.renderJson(json(null,1,"请选择要上传的文件"));
this.printFailure(1, "请选择要上传的文件");
return;
}
if(originalFilename.endsWith(".doc")||originalFilename.endsWith(".docx")||
originalFilename.endsWith(".pdf")||originalFilename.endsWith(".xlsx")||originalFilename.endsWith(".xls")||
originalFilename.endsWith(".ppt")||originalFilename.endsWith(".pptx")){
String uploadPath = "upload/document"+ "/" + UUID.getUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
// String uploadPath = "upload/document"+ "/" + UUID.getUUID()+originalFilename;
FileStorageHelper.transferFile(upExcel.getInputStream(), uploadPath);
Document document = new Document();
String[] split = originalFilename.split("\\.");
String string = split[0];
document.setDesn(string);
document.setUrl(uploadPath);
String id = UUID.getUUID();
document.setAuthor(UserTokenHelper.getUserId());
document.setDocumentID(id);
documentService.save(document);
// return id;
// this.renderJson(json(id,0,"上传文件成功"));
this.printSuccess(id,"上传文件成功");
return;
}else{
// return "请选择正确格式的文档";
// this.renderJson(json(null,2,"请选择正确格式的文档"));
this.printFailure(2,"请选择正确格式的文档");
return;
}
} catch (Exception e) {
e.printStackTrace();
// return "上传文件失败";
// this.renderJson(json(null,3,"上传文件失败"));
this.printFailure(3,"上传文件失败");
return;
}
}
下载文件
/**
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
* 下载文件
* @param urldata 文件路径
* @param response
*/
@RequestMapping(value = "/download", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public void download(String urldata,String name,HttpServletResponse response){
// String rootPath = SpringMVCUtil.getRequest().getSession().getServletContext().getRealPath("/");// 获取项目根目录
try {
File file = new File(urldata);// path是根据日志路径和文件名拼接出来的
String filename = file.getName();// 获取文件名称
String[] split = filename.split("\\.");
String string = split[1];
String nameNew = name+"."+string;
InputStream fis = new BufferedInputStream(new FileInputStream(urldata));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
response.reset();
// 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
//response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
response.addHeader("Content-Disposition", "attachment;filename=" + new String(nameNew.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
response.addHeader("Content-Length", "" + file.length());
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(buffer);// 输出文件
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}