我有一個表單上傳文件。我的控制器: -上傳文件,zip並將其保存到內存
public class ConsentController {
@Autowired
private ConsentRepository consentRepository;
private static String UPLOADED_FOLDER = "E://java//files//"; //this directory is used to save the uploaded file
public String filepath;
@RequestMapping(value="/addconsent",headers=("content-type=multipart/*"),method = RequestMethod.POST)
public String addConsent(@RequestParam("consentstatus") String consentStatus,
@RequestParam("file")MultipartFile file,
@RequestParam("pid")Participants pid,
@RequestParam("participantsid") long participantsid,
@RequestParam("userid")User userid,
@RequestParam("consentid") long consentid
){
if(consentRepository.findByParticipants(pid)==null){
if(file.isEmpty()) {
Consent consent = new Consent(consentStatus,"null",userid,pid);
consentRepository.save(consent);
}
else{
try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
Files.write(path, bytes);
//get the path of the saved filed
String filename = file.getOriginalFilename();
String filepath = Paths.get(UPLOADED_FOLDER, filename).toString();
this.filepath=filepath;
Consent consent=new Consent(consentStatus,this.filepath,userid,pid);
consentRepository.save(consent);
}catch (Exception ex){
System.out.println("Error"+ex.getMessage());
}
}
}
此代碼完美上傳和存儲我上傳的文件在e驅動器。但現在我想在將文件保存到目錄之前壓縮文件。現在,如果我上傳images.jpg,它會上傳images.jpg。我想這個images.jpg被保存爲(任何名字),但以壓縮格式。
只是做它解壓縮文件。 [示例1](http://www.journaldev.com/957/java-zip-file-folder-example) - [示例2](http://stackoverflow.com/questions/37404541/create-one-zip -file-使用-A-設置的-PDF文件) – Patrick