use of org.springframework.web.multipart.MultipartFile in project dhis2-core by dhis2.
the class FileResourceUtils method toTempFile.
/**
* Transfers the given multipart file content to a local temporary file.
*
* @param multipartFile the multipart file.
* @return a temporary local file.
* @throws IOException if the file content could not be transferred.
*/
public static File toTempFile(MultipartFile multipartFile) throws IOException {
File tmpFile = Files.createTempFile("org.hisp.dhis", ".tmp").toFile();
tmpFile.deleteOnExit();
multipartFile.transferTo(tmpFile);
return tmpFile;
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class MultipartRequestMatchersTests method testResourceMatch.
@Test
public void testResourceMatch() throws Exception {
MultipartFile f1 = new MockMultipartFile("f1", "foo.txt", "text/plain", "Foo Lorem ipsum".getBytes());
MultipartFile f2 = new MockMultipartFile("f2", "bar.txt", "text/plain", "Bar Lorem ipsum".getBytes());
MultipartFile f3 = new MockMultipartFile("f3", "foobar.txt", "text/plain", "Foobar Lorem ipsum".getBytes());
this.input.add("fooParam", "foo value");
this.input.add("barParam", "bar value");
this.input.add(f1.getName(), f1.getResource());
this.input.add(f2.getName(), f2.getResource());
this.input.add(f3.getName(), f3.getResource());
this.expected.addAll(this.input);
writeAndAssert();
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class MultipartRequestMatchersTests method testByteArrayNoMatch.
@Test
public void testByteArrayNoMatch() throws Exception {
MultipartFile f1 = new MockMultipartFile("f1", "foo.txt", "text/plain", "Foo Lorem ipsum".getBytes());
MultipartFile f2 = new MockMultipartFile("f2", "bar.txt", "text/plain", "Bar Lorem ipsum".getBytes());
this.input.add("fooParam", "foo value");
this.input.add("barParam", "bar value");
this.input.add(f1.getName(), f1.getResource());
this.input.add(f2.getName(), f2.getResource());
this.expected.addAll(this.input);
this.expected.set(f1.getName(), f2.getBytes());
assertThatExceptionOfType(AssertionError.class).isThrownBy(this::writeAndAssert);
}
use of org.springframework.web.multipart.MultipartFile in project Settler by EmhyrVarEmreis.
the class ImportService method importTransactions.
public void importTransactions(MultipartFile file) throws IOException {
List<Category> categoryList = categoryRepository.findAll();
Map<String, Category> categoryMap = categoryList == null ? new HashMap<>() : categoryList.stream().collect(Collectors.toMap(Category::getCode, category -> category));
List<Transaction> transactionList = new ArrayList<>();
ByteArrayInputStream stream = new ByteArrayInputStream(file.getBytes());
String content = IOUtils.toString(stream, "UTF-8");
String[] lines = content.split("\\r?\\n");
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("dd.MM.yyyy");
User o = userRepository.findOneByLogin(splitLine(lines[0])[26]);
User c = userRepository.findOneByLogin(splitLine(lines[0])[27]);
int ln = 0;
for (String line : lines) {
ln++;
if (ln < 3) {
continue;
}
try {
String[] cells = splitLine(line);
String name = cells[0];
String mark = cells[4].trim();
Double value;
Double sharedAValue;
Double sharedBValue;
Integer sharedA;
Integer sharedB;
LocalDateTime date;
Boolean normal;
Transaction transaction = new Transaction();
List<Redistribution> owners = new ArrayList<>();
List<Redistribution> contractors = new ArrayList<>();
value = Double.valueOf(cells[2].replaceAll("[^0-9^,-]+", "").replace(",", "."));
date = dateTimeFormatter.parseLocalDateTime(cells[1]);
sharedA = cells.length < 7 || cells[5].trim().isEmpty() ? -1 : Integer.valueOf(cells[5].trim());
sharedB = cells.length < 7 || cells[6].trim().isEmpty() ? -1 : Integer.valueOf(cells[6].trim());
normal = value > 0;
value = Math.abs(value);
if (mark.equalsIgnoreCase("x")) {
if (sharedA < 0 || sharedB < 0) {
sharedA = 1;
sharedB = 2;
}
} else if (mark.equalsIgnoreCase("b") || mark.equalsIgnoreCase("z")) {
sharedA = 1;
sharedB = 1;
} else {
continue;
}
sharedAValue = value;
value = (value / (sharedA)) * sharedB;
value = (double) Math.round(value * 100) / 100;
sharedBValue = value - sharedAValue;
sharedBValue = (double) Math.round(sharedBValue * 100) / 100;
if (normal) {
owners.add(new Redistribution(RedistributionType.O, transaction, o, value));
if (sharedBValue > 0) {
contractors.add(new Redistribution(RedistributionType.C, transaction, o, sharedBValue));
}
contractors.add(new Redistribution(RedistributionType.C, transaction, c, sharedAValue));
} else {
owners.add(new Redistribution(RedistributionType.O, transaction, c, value));
contractors.add(new Redistribution(RedistributionType.C, transaction, o, sharedAValue));
if (sharedBValue > 0) {
contractors.add(new Redistribution(RedistributionType.C, transaction, c, sharedBValue));
}
}
transaction.setValue(value);
owners.forEach(redistribution -> redistribution.setPercentage(redistribution.getPercentage() / 1.0 / transaction.getValue()));
contractors.forEach(redistribution -> redistribution.setPercentage(redistribution.getPercentage() / 1.0 / transaction.getValue()));
transaction.setOwners(owners);
transaction.setContractors(contractors);
date = date.withHourOfDay(12).withMinuteOfHour(0).withSecondOfMinute(0);
transaction.setCreator(Security.currentUser());
transaction.setType(TransactionType.NOR);
transaction.setDescription(name);
transaction.setEvaluated(date);
List<Category> cl = checkCategories(transaction.getDescription()).stream().map(categoryMap::get).filter(Objects::nonNull).collect(Collectors.toList());
transaction.setCategories(cl.isEmpty() ? null : cl);
transaction.setReference(sequenceManager.getNextReferenceForTransaction(transaction));
transactionList.add(transaction);
} catch (Exception e) {
log.warn("Unable to convert line No{}: {}", ln, line, e);
}
}
transactionList.sort((o1, o2) -> o1.getEvaluated().compareTo(o2.getCreated()));
transactionList.forEach(t -> {
t.setCreated(new LocalDateTime());
log.info(t.getEvaluated() + " " + t.getReference() + " " + t.getValue() + " [" + t.getDescription() + "]" + " [" + t.getOwners().stream().map(r -> r.getId().getUser().getLogin() + "/" + r.getPercentage()).collect(Collectors.joining(", ")) + "]" + " [" + t.getContractors().stream().map(r -> r.getId().getUser().getLogin() + "/" + r.getPercentage()).collect(Collectors.joining(", ")) + "]" + " [" + (t.getCategories() == null ? "" : t.getCategories().stream().map(Category::getCode).collect(Collectors.joining(", "))) + "]");
transactionRepository.save(t);
});
}
use of org.springframework.web.multipart.MultipartFile in project gocd by gocd.
the class ArtifactsController method postArtifact.
@RequestMapping(value = "/repository/restful/artifact/POST/*", method = RequestMethod.POST)
public ModelAndView postArtifact(@RequestParam("pipelineName") String pipelineName, @RequestParam("pipelineCounter") String pipelineCounter, @RequestParam("stageName") String stageName, @RequestParam(value = "stageCounter", required = false) String stageCounter, @RequestParam("buildName") String buildName, @RequestParam(value = "buildId", required = false) Long buildId, @RequestParam("filePath") String filePath, @RequestParam(value = "attempt", required = false) Integer attempt, MultipartHttpServletRequest request) throws Exception {
JobIdentifier jobIdentifier;
if (!headerConstraint.isSatisfied(request)) {
return ResponseCodeView.create(HttpServletResponse.SC_BAD_REQUEST, "Missing required header 'Confirm'");
}
if (!isValidStageCounter(stageCounter)) {
return buildNotFound(pipelineName, pipelineCounter, stageName, stageCounter, buildName);
}
try {
jobIdentifier = restfulService.findJob(pipelineName, pipelineCounter, stageName, stageCounter, buildName, buildId);
} catch (Exception e) {
return buildNotFound(pipelineName, pipelineCounter, stageName, stageCounter, buildName);
}
int convertedAttempt = attempt == null ? 1 : attempt;
try {
File artifact = artifactsService.findArtifact(jobIdentifier, filePath);
if (artifact.exists() && artifact.isFile()) {
return FileModelAndView.fileAlreadyExists(filePath);
}
MultipartFile multipartFile = multipartFile(request);
if (multipartFile == null) {
return FileModelAndView.invalidUploadRequest();
}
boolean success = saveFile(convertedAttempt, artifact, multipartFile, shouldUnzipStream(multipartFile));
if (!success) {
return FileModelAndView.errorSavingFile(filePath);
}
success = updateChecksumFile(request, jobIdentifier, filePath);
if (!success) {
return FileModelAndView.errorSavingChecksumFile(filePath);
}
return FileModelAndView.fileCreated(filePath);
} catch (IllegalArtifactLocationException e) {
return FileModelAndView.forbiddenUrl(filePath);
}
}
Aggregations