Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java存储文件_Java_Spring_Rest - Fatal编程技术网

用Java存储文件

用Java存储文件,java,spring,rest,Java,Spring,Rest,我有一个web UI和一个基于spring的rest服务。UI有两个按钮upload和download。当调用upload时,调用我的rest服务。rest服务将获取文件并将其存储在机器中 浏览器(上传文件)--->服务(获取文件并存储文件) 如何将接收到的文件存储在设备上 @RequestMapping(value = "/file", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE}, method = RequestMethod.

我有一个web UI和一个基于spring的rest服务。UI有两个按钮upload和download。当调用upload时,调用我的rest服务。rest服务将获取文件并将其存储在机器中

浏览器(上传文件)--->服务(获取文件并存储文件)

如何将接收到的文件存储在设备上

@RequestMapping(value = "/file", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE}, method = RequestMethod.POST)
    public void putFile(@RequestParam(value="fileName", required=false) File fileName,HttpServletRequest request,HttpServletResponse response) throws IOException{

    What do I do here ,How can I store the received file .
    }

作为请求的一部分,我的javascript是否可以将文件发送到服务器?

您可以使用MultipartFile在Spring中获取实际文件

@RequestMapping(method = RequestMethod.POST, value = "/upload")
public String handleFileUpload(@RequestParam("name") String name,
                               @RequestParam("file") MultipartFile file) {
    ...
}

是正确的示例。

假设该文件在请求正文中可用,可能类似以下内容:

InputStream requestInputStream = request.getInputStream();
OutputStream fileOutputStream = new FileOutputStream(fileName);

final int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];

int byteCount;
while( (byteCount=requestInputStream.read(buffer)) != 0 ) {
    fileOutputStream.write(buffer, /*offset = 0*/ 0, byteCount);
}

fileOutputStream.close();
试试这个

@RequestMapping(value = "/file", method = RequestMethod.POST)
public void putFile(HttpServletRequest request, HttpServletResponse response, @RequestParam CommonsMultipartFile[] fileUpload) throws Exception {
    InputStream templateInputStream = null;
    if (fileUpload != null && fileUpload.length > 0) {
        for (CommonsMultipartFile aFile : fileUpload) {
            templateInputStream = aFile.getInputStream();
            convertStreamToFile(templateInputStream, new File("/home/user/FileStorage/<filename>"));
            templateInputStream.reset();
            break;
        }
    }
}

    public void convertStreamToFile(InputStream is, File file) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        BufferedWriter fileWriter = new BufferedWriter(new FileWriter(file));
        String line = null;
        while ((line = reader.readLine()) != null) {
            fileWriter.write(line + "\n");
        }
        fileWriter.flush();
        fileWriter.close();
        is.close();

    }
@RequestMapping(value=“/file”,method=RequestMethod.POST)
public void putFile(HttpServletRequest请求、HttpServletResponse响应、@RequestParam commonmultipartfile[]fileUpload)引发异常{
InputStream templateInputStream=null;
if(fileUpload!=null&&fileUpload.length>0){
用于(CommonsMultipartFile文件:fileUpload){
templateInputStream=aFile.getInputStream();
convertStreamToFile(templateInputStream,新文件(“/home/user/FileStorage/”);
templateInputStream.reset();
打破
}
}
}
public void convertStreamToFile(InputStream为,文件文件)引发IOException{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
BufferedWriter fileWriter=新的BufferedWriter(新的fileWriter(文件));
字符串行=null;
而((line=reader.readLine())!=null){
fileWriter.write(第+行“\n”);
}
fileWriter.flush();
fileWriter.close();
is.close();
}

如何将其存储在某个位置,例如/home/user/FileStorageOutputStream fileOutputStream=newfileoutputstream(new File(“/home/user/FileStorage/”);您可以编写基于Spring的REST客户端,但不知道如何将数据写入文件?或者你认为这是一个代码编写服务?