ファイルをアップロードするフォームがあります。私のコントローラ:ファイルをアップロードし、圧縮してメモリに保存します。
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を(任意の名前で)zip形式で保存します。ここで
内のファイルを圧縮して解凍する方法を示していますチュートリアルです。 [例1](http://www.journaldev.com/957/java-zip-file-folder-example) - [例2](http://stackoverflow.com/questions/37404541/create-one-zip) -file-as-of-pdf-filesを使用したファイル) – Patrick