| | |
| | | import com.google.common.collect.Lists; |
| | | import com.nq.common.ServerResponse; |
| | | import com.nq.service.IFileUploadService; |
| | | import com.nq.utils.FTPUtil; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.util.UUID; |
| | | |
| | | import com.nq.utils.PropertiesUtil; |
| | | import com.nq.utils.ftp.FTPUtil; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | |
| | | |
| | | public ServerResponse upload(MultipartFile file, String path) { |
| | | |
| | | String fileName = file.getOriginalFilename(); |
| | | |
| | | |
| | | String fileExtentionName = fileName.substring(fileName.lastIndexOf(".") + 1); |
| | | |
| | | |
| | | String uploadFileName = UUID.randomUUID() + "." + fileExtentionName; |
| | | |
| | | |
| | | File fileDir = new File(path); |
| | | |
| | | |
| | | if (!fileDir.exists()) { |
| | | |
| | | |
| | | fileDir.setWritable(true); |
| | | |
| | | fileDir.mkdirs(); |
| | | |
| | | } |
| | | |
| | | |
| | | File tartgetFile = new File(path, uploadFileName); |
| | | |
| | | boolean result = false; |
| | | |
| | | File tartgetFile = new File(PropertiesUtil.getProperty("ftp.address"), uploadFileName); |
| | | try { |
| | | |
| | | file.transferTo(tartgetFile); |
| | | |
| | | |
| | | result = FTPUtil.uploadFile(Lists.newArrayList(new File[]{tartgetFile})); |
| | | |
| | | |
| | | tartgetFile.delete(); |
| | | |
| | | } catch (Exception e) { |
| | | |
| | | log.error("上传文件异常 , 错误信息 = {}", e); |
| | | |
| | | return null; |
| | | |
| | | } |
| | | |
| | | |
| | | if (result) { |
| | | |
| | | return ServerResponse.createBySuccess(tartgetFile.getName()); |
| | | |
| | | } catch (IOException e) { |
| | | return ServerResponse.createByErrorMsg("上传失败"); |
| | | } |
| | | |
| | | return ServerResponse.createByErrorMsg("上传失败"); |
| | | |
| | | } |
| | | |