0% found this document useful (0 votes)
2 views

ImpCode

The document provides a Java Spring application for uploading and downloading files using RESTful services. It includes an UploadController for handling file uploads and an ImageResource for downloading images from a server location. Additionally, it contains Bash scripts for reading hostnames from a file and generating JSON strings based on user credentials.

Uploaded by

aravindhkumar311
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ImpCode

The document provides a Java Spring application for uploading and downloading files using RESTful services. It includes an UploadController for handling file uploads and an ImageResource for downloading images from a server location. Additionally, it contains Bash scripts for reading hostnames from a file and generating JSON strings based on user credentials.

Uploaded by

aravindhkumar311
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

UPLOAD FILE IN REST

======================

package com.mkyong.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
public class UploadController {

//Save the uploaded file to this folder


private static String UPLOADED_FOLDER = "F://temp//";

@GetMapping("/")
public String index() {
return "upload";
}

@PostMapping("/upload") // //new annotation since 4.3


public String singleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {

if (file.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "Please select a file
to upload");
return "redirect:uploadStatus";
}

try {

// Get the file and save it somewhere


byte[] bytes = file.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
Files.write(path, bytes);

redirectAttributes.addFlashAttribute("message",
"You successfully uploaded '" + file.getOriginalFilename() +
"'");

} catch (IOException e) {
e.printStackTrace();
}

return "redirect:/uploadStatus";
}

@GetMapping("/uploadStatus")
public String uploadStatus() {
return "uploadStatus";
}

Download file with rest


=======================

import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
@RequestMapping("/app")
public class ImageResource {

private static final String EXTENSION = ".jpg";


private static final String SERVER_LOCATION = "/server/images";

@RequestMapping(path = "/download", method = RequestMethod.GET)


public ResponseEntity<Resource> download(@RequestParam("image") String image)
throws IOException {
File file = new File(SERVER_LOCATION + File.separator + image + EXTENSION);

HttpHeaders header = new HttpHeaders();


header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;
filename=img.jpg");
header.add("Cache-Control", "no-cache, no-store, must-revalidate");
header.add("Pragma", "no-cache");
header.add("Expires", "0");

Path path = Paths.get(file.getAbsolutePath());


ByteArrayResource resource = new
ByteArrayResource(Files.readAllBytes(path));

return ResponseEntity.ok()
.headers(header)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);
}
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

The architecture, or processor, for which to compile code.Examples are amd64,


386, arm, ppc64
independent executables (PIE)
The operating system for which to compile code

-buildmode=pie

#!/bin/bash
# A shell script to read file line by line

filename="/Users/vamsi.c/Documents/hostnames.txt"
echo "started"
arr=()
while IFS= read -r line; do
arr+=("$line")
done < $filename
echo $arr

#!/bin/bash
USERNAME="vamsi"
filename="/Users/vamsi.c/Documents/hostnames.txt"
declare -a myArray
myArray=(`cat "$filename"`)
for (( i = 0 ; i < ${#myArray[*]} ; i++))
do
echo "Element [$i]: ${myArray[$i]}"
done
template='{"hostname":"{"%s"}","username":"%s"}'

json_string=$(printf "$template" "${myArray[*]}" "$USERNAME" )

echo "$json_string"

============================-=============-===============-=============-
=================

#!/bin/bash
USERNAME="vamsi"
USERPASSWORD="@Kavya143"
AppACCOUNTNAME="rdrt"
APPACCOUNTPASSWORD="@Kavya143"
NOTES="Control Test"
filename="/Users/vamsi.c/Documents/demohost"
declare -a myArray
myArray=(`cat "$filename"`)
Qarr=()
j=1
count=$((${#myArray[*]}-$j))

for (( i = 1 ; i < ${#myArray[*]} ; i++))


do
# access each element as $i
if [ $i -eq $count ]
then
template='"%s"'
k=$(printf "$template" "${myArray[$i]}")
Qarr+=$k
else
template='"%s",'
k=$(printf "$template" "${myArray[$i]}")
Qarr+=$k
fi
done
# echo ${Qarr[@]}
template='{"resource":{"hostname":
[%s],"username":"%s","userPassword":"%s","appAccountName":
"%s","appAccountPassword": "%s","notes": "%s"}'

json_string=$(printf "$template" "${Qarr[*]}" "$USERNAME" "$USERPASSWORD"


"$AppACCOUNTNAME" "$APPACCOUNTPASSWORD" "NOTES")

echo "$json_string"
cat <<EOF > /Users/vamsi.c/Documents/jsonbody1.txt
"$json_string"
EOF
# sed -i '$json_string/}/' "/Users/vamsi.c/Documents/jsonbody.txt"

You might also like