[英]get file from POST request using javax library
我正在嘗試將 AWS SDK 連接到用 JAVA EE 編寫的項目。 我寫了一個服務,它與 aws 服務器交互。 我能夠創建、刪除、上傳存儲桶/文件。 一旦我直接從文件系統發送文件,一切都很好。
在其他類中,我試圖從頁面的 POST 方法接收文件,例如:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="file" onchange="handler(this.files[0])" />
<script type="text/javascript">
const handler = (file) => {
const formData = new FormData();
formData.append(file.name, file);
const requestOptions = {
method: 'POST',
mode: 'no-cors',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'form/multipart'
},
body: formData
};
// Здесь ссылку на команду
fetch(`http://localhost:8800/files/setVideo`, requestOptions)
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.log(err))
};
</script>
</body>
</html>
我的控制器方法如下所示:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import javax.ws.rs.POST;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.json.JsonObject;
import javax.json.Json;
import javax.json.JsonReader;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
@Path("files")
public class FilesWork{
@Context
private UriInfo context;
@POST
@Path(value = "/setVideo")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public String UploadFileToCloud( what to write here? ) throws FileNotFoundException, IOException, ServletException {
String tempFilePath = "/tempFiles"; //dir for temp files
String fileName = "temp"; //file name to create temp file
File FileExt = new File(tempFilePath, fileName); //create empty file
fileName = FunctionsGlobal.extractFileName(part); //get real file name
InputStream IOFile = part.getInputStream(); //get steam from part
Files.copy(IOFile, FileExt.toPath(), StandardCopyOption.REPLACE_EXISTING); //rewrite temp file with real from stream
AWSUtils s3 = new AWSUtils();
JsonObject UrlBuilder = Json.createObjectBuilder().add("Link", "some").build();
return FunctionsGlobal.returnDataAll(UrlBuilder);
}
}
我嘗試使用 javax.servlet.http.Part 獲取文件,但從服務器拋出 HTTP 狀態 415 - 不支持的媒體類型。
如果我嘗試這樣寫:
public String UploadFileToCloud(File file){}
然后我實際上有一個文件,但它看起來像 rep7876328953083992008tmp,我不知道如何將它更改為正常格式。
因此,主要問題是如何使用 javax 庫從“form/multipart”中形成的 POST 請求正確獲取文件。
提前謝謝大家,祝大家玩得開心!
在我的案例中,處理文件的解決方案是在這里找到的。 這是我用來從多部分/表單數據請求中獲取文件和/或標志的自定義 MultipartRequestMap 實現
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.