Mysql 使用primefaces将图像上载到服务器公共_html文件夹,并将图像路径保存在数据库中
我正在学习这个教程 它工作正常,一切正常,但我真正需要的是,将图像保存在服务器文件管理器中,并将其路径保存在数据库中 我尝试了这个方法,它只有在服务器中有一个php文件来处理它时才有效。因为这个要求,我不能用 爪哇:Mysql 使用primefaces将图像上载到服务器公共_html文件夹,并将图像路径保存在数据库中,mysql,html,jakarta-ee,jsf-2,primefaces,Mysql,Html,Jakarta Ee,Jsf 2,Primefaces,我正在学习这个教程 它工作正常,一切正常,但我真正需要的是,将图像保存在服务器文件管理器中,并将其路径保存在数据库中 我尝试了这个方法,它只有在服务器中有一个php文件来处理它时才有效。因为这个要求,我不能用 爪哇: 我的问题是,如何在不使用php的情况下完成请求的任务?还有别的办法吗?喜欢html fie吗?还是jsp 那个博客/文章/教程或任何使用的东西。将文件上载到服务器的磁盘文件系统并将其名称保存到数据库中不需要这样的代码。类路径上只需要库ApacheCommonsFileUpload及
我的问题是,如何在不使用php的情况下完成请求的任务?还有别的办法吗?喜欢html fie吗?还是jsp 那个博客/文章/教程或任何使用
的东西。将文件上载到服务器的磁盘文件系统并将其名称保存到数据库中不需要这样的代码。类路径上只需要库ApacheCommonsFileUpload及其依赖项ApacheCommonsIO。库与框架一起处理引擎罩下的所有其他脏任务。只需要几个函数来读取和写入磁盘文件系统中的文件流。我正在搜索,唯一发现的是他们将文件上传到本地文件夹,而不是服务器,比如,你有教程或其他什么吗@TinyFile实际上被上传到服务器上的固定位置。不应将它们上载到已部署的WAR文件(您可能指的是“本地文件夹”),因为WAR文件不用于永久文件存储,并且如果重新部署应用程序,这些上载的文件将丢失,请参见将文件上载到服务器上的固定位置(固定路径)-private final String UPLOAD\u DIRECTORY=“C:/uploads”代码>使用Apache Commons文件上载库。
//uploading the image to the web server
public void uploadImg(String path){
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String pathToOurFile = path ;
//host url
String urlServer = "/UploadImage.php"; // here should be the url to the php file in the server
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;{
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs.
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// Set HTTP method to POST.
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
outputStream.writeBytes("Content-Type: image/jpeg" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
catch (Exception ex)
{
//Exception handling
}}
}