import com.qtong.afin.core.util.ConfigProperties;
import com.qtong.afin.core.util.ftp.Doc2Html;
import com.qtong.afin.core.util.ftp.FtpHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/fileTest")
public class FileTestController {
/**
* 日誌
*/
public static Logger logger = LoggerFactory.getLogger(FileTestController.class);
@Autowired
ConfigProperties configProperties;
/**
* 文件在線預覽`
*/
@RequestMapping(value = "/findFile")
@ResponseBody
public void findContract(String newName, String url,HttpServletRequest request, HttpServletResponse response){
//h:/ftp_upload/ opt/test/ *.doc
String path1=configProperties.getFtpDownPath()+url+newName;
//h:/ftp_upload/ opt/test/
String path2=configProperties.getFtpDownPath()+configProperties.getFtpPath();
String filePri=newName.substring(newName.lastIndexOf(".")+1);//文件後綴名
String fileName=newName.substring(0,newName.lastIndexOf("."));//文件名
if("pdf".equals(filePri)){//查看是否是PDF文件
File file=new File(path1);
if (file.exists()){
byte[] data = null;
try {
FileInputStream input = new FileInputStream(file);
data = new byte[input.available()];
input.read(data);
response.getOutputStream().write(data);
input.close();
} catch (Exception e) {
logger.info("pdf處理文件異常"+e);
}
}else{
return;
}
}else {//非PDF文件
File file=new File(path2+fileName+".pdf");
if(!file.exists()){//查看之前是否轉過PDF文件,轉過則就可能存在,否則則就轉PDF文件,轉PDF文件使用軟件是openoffice(應該是這個名)
//轉成PDF文件
file = Doc2Html.uploadFile(path2, path1, "agreementfilepdf");
}
//返回文件流
if (file.exists()){
byte[] data = null;
try {
FileInputStream input = new FileInputStream(file);
data = new byte[input.available()];
input.read(data);
response.getOutputStream().write(data);
input.close();
} catch (Exception e) {
logger.info("pdf處理文件異常"+e);
}
}else{
return;
}
}
}
/**
* 合同文件下載,非ftp下載
*/
@RequestMapping(value = "/downFile")
@ResponseBody
public void download(String fileName,String newName,String url,HttpServletRequest request,HttpServletResponse response) {
try {
String path =configProperties.getFtpDownPath()+ url+newName;//文件所在地址,newName是實際存放的文件名
// 讀到流中
InputStream inStream = new FileInputStream(path);//文件的存放路徑
// 設置輸出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");//這裏的文件名是指客戶端下載後保存文件的文件名
// 循環取出流中的數據
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0) response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 文件上傳
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/uploadFile")
@ResponseBody
public String addAgreement2(@RequestParam(value = "age", required = true)String age,
String names,
HttpServletRequest request,
HttpServletResponse response) {
logger.info("接收到的參數爲:age="+age+"names="+names);//傳的其他參數
List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");//獲取前臺傳過來的文件集
for (MultipartFile file : files) {
if (files.isEmpty()) {//判斷文件是否非空
continue;
}
//文件上傳操作
//文件名
//獲取文件名字 文件原名 文件新名newName
String oldNames = file.getOriginalFilename();//獲取文件名字
int i = oldNames.lastIndexOf(".");
String substring = oldNames.substring(i);//截取文件後綴名
Long l = System.currentTimeMillis();
String fileName2 = l.toString();
String newName = fileName2 + substring;//形成新的文件名
//上傳路徑
String path = configProperties.getFtpPath();
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
//執行文件上傳操作
FtpHelper.uploadFile(configProperties.getFtpUrl(), Integer.valueOf(configProperties.getFtpPort()), configProperties.getFtpUsername(), configProperties.getFtpPassword(), path, newName, inputStream);
Map<String, Object> map = new HashMap<>();
map.put("oldName", oldNames);//文件原來的名字
map.put("newName", newName);//文件的新名字
map.put("path", path);
logger.info(map.toString());
}
//根據結果返回結果集
return "success";
}
}
|