For XML files, just for the controller
Controller.java
@Autowired
ResourceLoader resourceLoader;
@RequestMapping(value = "/file_download", produces = MediaType.APPLICATION_XML_VALUE)
public Resource xmlfile() {
Resource resource = resourceLoader.getResource("classpath:sample.xml");
try{
return new FileSystemResource(resource.getFile().toString());
} catch (FileNotFoundException e) {
e.getStackTrace();
}
}
If you write something like that, you can go, but this is displayed in the browser and it ends, so After all I want it in the download folder. Make a note of how to do it
Three processes are performed like this. Finally, return an appropriate view (null is fine) to complete the download function.
First, let's set the file. Set the file in ** src / main / resources **.
This time, set this sample.xml
Create the part of the controller that reads the file.
XMLController.java
@RestController
public class XMLController {
@Autowired
ResourceLoader resourceLoader;
@RequestMapping(value="/personize", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public String personize(HttpServletResponse response) {
Resource resource = resourceLoader.getResource("classpath:sample.xml");
}
}
This is all you need to read the file. ** ResourceLoader is automatically registered in the DI container when the context is created ** (like), so there is no need to even describe it in the xml file. Thank you @Autowired.
Let's divide the processing. Create a new private method.
XMLController.java
/**
*Convert from InputStream to byte string
* @param filepath
* @return
*/
private byte[] StreamToByte(Resource resource) {
int nRead;
InputStream is = null;
byte[] fileContent = new byte[16384];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
//Convert file to byte format
try {
is = new FileInputStream(resource.getFile().toString());
while ((nRead = is.read(fileContent, 0, fileContent.length)) != -1) {
buffer.write(fileContent, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
} catch (FileNotFoundException e) {
e.getStackTrace();
} catch (IOException e) {
//TODO auto-generated catch block
e.printStackTrace();
}
return null;
}
What Spring is doing is only the ** resource.getFile (). ToString () ** part of line 15. This is the ** getFile () ** method, which allows you to get the files under src / main / resource in File format. It is affirmative that it is converted to an absolute address by toString ().
XMLController.java
while ((nRead = is.read(fileContent, 0, fileContent.length)) != -1) {
buffer.write(fileContent, 0, nRead);
}
buffer.flush();
This is the main part of writing to BufferdOutputStream. Data is stored in the buffer and executed in flash. It looks like C, but it's already a fixed phrase. After that, I got the exception with try ~ catch.
Write to the output stream of response First of all, let's set the header of what kind of file.
XMLController.java
@RequestMapping(value="/personize", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public String personize(HttpServletResponse response) {
Resource resource = resourceLoader.getResource("classpath:sample.xml");
byte[] fileContent = null;
fileContent = StreamToByte(resource);
// //File writing
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + "sample.xml");
response.setContentLength(fileContent.length);
}
setContentType specifies a type called a binary file. The file name is specified in response.setHeader, but this is the name of the file that is actually downloaded. response.setContentLength is the size of the file.
Now let's write the created binary to OutputStream This is also written in another method.
XMLController.java
/**
*Write download file
* @param response
* @param fileContent
*/
public void OutputSreamWrite(HttpServletResponse response, byte[] fileContent) {
OutputStream os = null;
try {
os = response.getOutputStream();
os.write(fileContent);
os.flush();
} catch (IOException e) {
e.getStackTrace();
}
}
There will be no particular difficulty. Write with os.write and run the stream with os.flush.
Incorporate this method into your controller.
XMLController.java
@RequestMapping(value="/personize", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public String personize(HttpServletResponse response) {
Resource resource = resourceLoader.getResource("classpath:sample.xml");
byte[] fileContent = null;
fileContent = StreamToByte(resource);
// //File writing
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + "sample.xml");
response.setContentLength(fileContent.length);
OutputSreamWrite(response, fileContent);
return null;
}
With that feeling, the final form of the controller will be like this
XMLController.java
package tsugaruinfo.controller;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class XMLController {
@Autowired
ResourceLoader resourceLoader;
DomController(){
}
@RequestMapping(value="/personize", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public String personize(HttpServletResponse response) {
Resource resource = resourceLoader.getResource("classpath:sample.xml");
byte[] fileContent = null;
fileContent = StreamToByte(resource);
// //File writing
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + "sample.xml");
response.setContentLength(fileContent.length);
OutputSreamWrite(response, fileContent);
return null;
}
/**
*Convert from InputStream to byte string
* @param filepath
* @return
*/
private byte[] StreamToByte(Resource resource) {
int nRead;
InputStream is = null;
byte[] fileContent = new byte[16384];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
//Convert file to byte format
try {
is = new FileInputStream(resource.getFile().toString());
while ((nRead = is.read(fileContent, 0, fileContent.length)) != -1) {
buffer.write(fileContent, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
} catch (FileNotFoundException e) {
e.getStackTrace();
} catch (IOException e) {
//TODO auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
*Write download file
* @param response
* @param fileContent
*/
public void OutputSreamWrite(HttpServletResponse response, byte[] fileContent) {
OutputStream os = null;
try {
os = response.getOutputStream();
os.write(fileContent);
os.flush();
} catch (IOException e) {
e.getStackTrace();
}
}
}
It's been a little longer. However, the troublesome processing is separated, so when creating a new download function from now on, I try to use the troublesome processing. Since I explain each process individually, I wondered if I should understand the small finished product rather than grasping the whole at once.
Let's access the actual address.
↓
I was able to download sample.xml from the server.
http://bookmount8.blog.fc2.com/blog-entry-49.html?
Recommended Posts