本文整理汇总了Java中org.springframework.web.multipart.MultipartFile.getContentType方法的典型用法代码示例。如果您正苦于以下问题:Java MultipartFile.getContentType方法的具体用法?Java MultipartFile.getContentType怎么用?Java MultipartFile.getContentType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.multipart.MultipartFile
的用法示例。
在下文中一共展示了MultipartFile.getContentType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uploadAvatar
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
@RequestMapping(value = "/{id}/uploadAvatar", method = RequestMethod.POST)
@ApiOperation(value = "Add new upload Avatar")
public String uploadAvatar(@ApiParam(value = "user Id", required = true) @PathVariable("id") Integer id,
@ApiParam(value = "Image File", required = true) @RequestPart("file") MultipartFile file) {
String contentType = file.getContentType();
if (!FileUploadUtil.isValidImageFile(contentType)) {
return "Invalid image File! Content Type :-" + contentType;
}
File directory = new File(AVATAR_UPLOAD.getValue());
if (!directory.exists()) {
directory.mkdir();
}
File f = new File(userService.getAvatarUploadPath(id));
try (FileOutputStream fos = new FileOutputStream(f)) {
byte[] imageByte = file.getBytes();
fos.write(imageByte);
return "Success";
} catch (Exception e) {
return "Error saving avatar for User " + id + " : " + e;
}
}
示例2: of
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
public static UserFile of(MultipartFile file, User user) {
UserFile userFile = new UserFile();
userFile.setName(file.getOriginalFilename());
userFile.setSize(file.getSize());
userFile.setUploader(user);
userFile.setPath(uuid() + "." + FilenameUtils.getExtension(file.getOriginalFilename()));
String mimeType = file.getContentType();
String type = mimeType.split("/")[0];
if (type.equalsIgnoreCase("image")) {
userFile.setFileType(FileType.IMAGE);
}
// TODO set filetype to DOC or ETC
ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC);
userFile.setUploadedAt(Date.from(utc.toInstant()));
return userFile;
}
示例3: upload
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
@PostMapping
public @ResponseBody FileInfo upload(@RequestParam("myFile") MultipartFile file,
@RequestParam("message") String message) throws Exception {
String uuid = UUID.randomUUID().toString();
String filePath = FILES_BASE + uuid;
FileUtils.copyToFile(file.getInputStream(), new File(filePath));
String filename = file.getOriginalFilename();
String contentType = file.getContentType();
FileInfo fileInfo = new FileInfo(uuid, filename, message, contentType);
String json = mapper.writeValueAsString(fileInfo);
FileUtils.writeStringToFile(new File(filePath + "_meta.txt"), json, "utf-8");
return fileInfo;
}
示例4: uploadLogo
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
@RequestMapping(value = "/{id}/uploadLogoAsFile", method = RequestMethod.POST)
@ApiOperation(value = "Add new upload Logo as Image File")
public String uploadLogo(@ApiParam(value = "Organization Id", required = true) @PathVariable Integer id,
@ApiParam(value = "Image File", required = true) @RequestPart("file") MultipartFile file) {
String contentType = file.getContentType();
if (!FileUploadUtil.isValidImageFile(contentType)) {
return "Invalid Image file! Content Type :-" + contentType;
}
File directory = new File(LOGO_UPLOAD.getValue());
if (!directory.exists()) {
directory.mkdir();
}
File f = new File(organizationService.getLogoUploadPath(id));
try (FileOutputStream fos = new FileOutputStream(f)) {
byte[] imageByte = file.getBytes();
fos.write(imageByte);
return "Success";
} catch (Exception e) {
return "Error saving logo for organization " + id + " : " + e;
}
}
示例5: Data
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
public Data(String id ,MultipartFile data,Weaver weaver){
this.id = id;
this.date = new Date();
this.weaver = weaver;
this.name= "";
try{
this.content= data.getBytes();
}catch(IOException e){
this.content= null;
}
this.name = data.getOriginalFilename();
this.name = this.name.replace(" ", "_");
this.name = this.name.replace("#", "_");
this.name = this.name.replace("?", "_");
this.name = this.name.trim();
this.type = data.getContentType();
this.filePath = path+weaver.getId()+File.separator+this.id+File.separator+this.name;
}
示例6: uploadFile
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
public static String uploadFile(MultipartFile file) throws IOException {
if (!file.isEmpty()) {
String today = LocalDate.now().toString();
String type = file.getContentType();
String suffix = "." + type.split("/")[1];
String userUploadPath = today + "/";
String fileName = UUID.randomUUID().toString()+suffix;
File file_dir = new File(Constants.UPLOAD_PATH + userUploadPath);
if (!file_dir.exists()) file_dir.mkdirs();
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(Constants.UPLOAD_PATH + userUploadPath + fileName)));
stream.write(file.getBytes());
stream.close();
return Constants.STATIC_URL+userUploadPath+fileName;
}
return null;
}
示例7: store
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
public String store(MultipartFile file, Integer size) {
try {
InputStream stream = file.getInputStream();
if (size != null && file.getContentType() != null && "image".equals(file.getContentType().substring(0, 5))) {
stream = ImageResizeUtil.resize(stream, size);
}
String filename =
UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(file.getOriginalFilename());
amazonS3Template.save(filename, stream);
IOUtils.closeQuietly(stream);
String prefix = String.format(applicationProperties.getAmazon().getAws().getTemplate(),
applicationProperties.getAmazon().getS3().getBucket());
return prefix + filename;
} catch (IOException e) {
log.error("Error storing file", e);
throw new BusinessException("Error storing file");
}
}
示例8: buildAvatarHttpEntity
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
public static HttpEntity<Resource> buildAvatarHttpEntity(MultipartFile multipartFile) throws IOException {
// result headers
HttpHeaders headers = new HttpHeaders();
// 'Content-Type' header
String contentType = multipartFile.getContentType();
if (StringUtils.isNotBlank(contentType)) {
headers.setContentType(MediaType.valueOf(contentType));
}
// 'Content-Length' header
long contentLength = multipartFile.getSize();
if (contentLength >= 0) {
headers.setContentLength(contentLength);
}
// File name header
String fileName = multipartFile.getOriginalFilename();
headers.set(XM_HEADER_CONTENT_NAME, fileName);
Resource resource = new InputStreamResource(multipartFile.getInputStream());
return new HttpEntity<>(resource, headers);
}
示例9: handleFileUpload
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
/**
* 上传
* @param file
* @param redirectAttributes
* @return
*/
@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
try {
File f = new File(file.getOriginalFilename(), file.getContentType(), file.getSize(), file.getBytes());
f.setMd5( MD5Util.getMD5(file.getInputStream()) );
fileService.saveFile(f);
} catch (IOException | NoSuchAlgorithmException ex) {
ex.printStackTrace();
redirectAttributes.addFlashAttribute("message",
"Your " + file.getOriginalFilename() + " is wrong!");
return "redirect:/";
}
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + file.getOriginalFilename() + "!");
return "redirect:/";
}
示例10: save
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
@SuppressWarnings("WeakerAccess")
public void save(MultipartFile multipartFile, String path) {
if (multipartFile == null || multipartFile.isEmpty()) {
throw new IllegalArgumentException("Multipart file is required.");
}
String contentType = multipartFile.getContentType();
validateFormat(contentType);
validateSize(multipartFile.getSize());
try {
doSave(multipartFile, path, contentType);
} catch (IOException e) {
LOGGER.error("Save image failed.", e);
throw new RuntimeException(e);
}
}
示例11: upload
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
private ServerResponse upload(MultipartFile avatar){
String contentType = avatar.getContentType();//获取上传文件的格式
String fileName=avatar.getOriginalFilename();
//检验上传文件的格式
if (!contentType.equals("image/jpeg") && !contentType.equals("image/png")) {
return ServerResponse.createErrorResponse(400,"Img must be *.jpg/jpeg/png");
}
try {
fileName=FileUploadUtil.upload(avatar.getBytes(),fileName);
} catch (IOException e) {
return ServerResponse.createErrorResponse(500,e.getMessage());
}
return ServerResponse.createSuccessResponse(fileName);
}
示例12: DocumentContentVersion
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
public DocumentContentVersion(StoredDocument item, MultipartFile file, Blob data) {
this.mimeType = file.getContentType();
setOriginalDocumentName(file.getOriginalFilename());
this.size = file.getSize();
this.documentContent = new DocumentContent(this, data);
this.storedDocument = item;
}
示例13: upload
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
/**
* Uploads a file.
*
* <p>
* File should be uploaded as multipart/form-data.
* </p>
* @param uploadFile Provided file with metadata
* @param index Should be the content of file indexed
* @return Reference to a stored file
*/
@ApiOperation(value = "Uploads a file and returns the reference to the stored file.",
notes = "File should be uploaded as multipart/form-data.",
response = FileRef.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successful response", response = FileRef.class)})
@RequestMapping(value = "/", method = RequestMethod.POST)
public FileRef upload(@ApiParam(value = "Provided file with metadata", required = true)
@RequestParam("file") MultipartFile uploadFile,
@ApiParam(value = "Should be the content of file indexed")
@RequestParam(name = "index", defaultValue = "false") Boolean index) {
try (InputStream stream = uploadFile.getInputStream()) {
String filename = uploadFile.getOriginalFilename();
if (filename != null) {
filename = FilenameUtils.getName(filename);
}
String contentType = uploadFile.getContentType();
return repository.create(stream, filename, contentType, index);
} catch (IOException e) {
throw new BadArgument("file");
}
}
示例14: doPost
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
return;
}
try {
MultipartHttpServletRequest
multiRequest =
new CommonsMultipartResolver().resolveMultipart(request);
MultiValueMap<String, MultipartFile> fileMap = multiRequest.getMultiFileMap();
BlobstoreService blobstoreService = AppFactory.get().getBlobstoreService();
Map<String, String> names = new HashMap<>(1);
for (String fieldName : fileMap.keySet()) {
MultipartFile file = fileMap.getFirst(fieldName);
String fileName = file.getOriginalFilename();
String contentType = file.getContentType();
long sizeInBytes = file.getSize();
String
blobKey =
blobstoreService.store(fileName, contentType, sizeInBytes, file.getInputStream());
names.put(fieldName, blobKey);
}
request.getSession().setAttribute("blobs", names);
RequestDispatcher
dispatcher =
getServletContext().getRequestDispatcher(request.getParameter("ru"));
dispatcher.forward(multiRequest, response);
} catch (Exception ex) {
_logger.severe("Upload failed", ex);
}
}
示例15: checkImageFile
import org.springframework.web.multipart.MultipartFile; //导入方法依赖的package包/类
private void checkImageFile(MultipartFile file) {
String mimeType = file.getContentType();
String type = mimeType.split("/")[0];
if (!type.equalsIgnoreCase("image"))
{
throw new IllegalArgumentException(file.getOriginalFilename() + " is not image file.");
}
}