gpt4 book ai didi

java - 使用jsp作为服务器端脚本将文件上传到特定文件夹

转载 作者:行者123 更新时间:2023-12-02 08:14:13 27 4
gpt4 key购买 nike

正如标题所示,我正在尝试将文件上传到本地服务器,为此我使用 JSP 和 uploadify(基于 flash 的 jquery 上传程序)。我已经使用 glassfish server 3.1 成功上传了一个文件,并将其作为我的 HTML 代码:

</head>
<body>
<table>
<tr>
<td>
<input id="file_upload" name="file_upload" type="file" />
<script type="text/javascript">
$('#file_upload').uploadify({
'uploader' : 'uploadify/uploadify.swf',
'script' : 'UploadFile/uploadFile.jsp',
'cancelImg' : 'uploadify/cancel.png',
'multi' : true,
'auto' : false,
'onComplete' : function(fileObj)
{
alert (fileObj.filePath); //The path on the server to the uploaded file
}
});
</script>

</td>
<td>
<a href="javascript:$('#file_upload').uploadifyUpload($('.uploadifyQueueItem').last().attr('id').replace('file_upload',''));">Upload Last File</a>
</td>
</tr>
</table>
</body>

这是我的服务器端脚本:

<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());

int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;

while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);

String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;

pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>

<table>
<tr>
<td>
<b>File uploaded:</b>
<% out.println(saveFile);%>
</td>
</tr>
</table>

<%} else {
out.println(contentType.indexOf("multipart/form-data"));
out.println(request.getContentType());
%>
<br/> error <% } %>

所以我的问题是,是否可以更改上传内容的默认文件夹?例如:我现在的默认文件夹是 C:\Users\USERNAME.netbeans\7.0\config\GF3\domain1 是否可以将其更改为 C:\Users\USERNAME\Desktop ?

如果我对问题不够清楚,请随时说出来,非常感谢任何帮助,谢谢。

最佳答案

My default folder right now is C:\Users\USERNAME.netbeans\7.0\config\GF3\domain1 is it possible to change it to C:\Users\USERNAME\Desktop ?

您的默认目录恰好是domain1目录,因为您没有在以下行中指定文件的绝对路径:

FileOutputStream fileOut = new FileOutputStream(saveFile);

如果没有绝对路径,文件将保存在相对于 Java 进程当前工作目录的位置,对于 Glassfish 应用程序服务器来说,该位置恰好是域目录。一旦指定绝对路径,您就可以将上传的文件保存到您选择的位置。

<小时/>

换句话来说,请考虑以下几点:

  • 使用Servlet处理文件上传请求。 JSP 通常用于生成 View 的演示内容。
  • 使用@MultipartConfig Servlet 3.0 API 提供的用于处理文件上传请求的注释; Tomcat 7 和 Glassfish 3.1 依赖于底层编写良好的 Apache Commons Fileupload 库来处理多部分 POST 请求。这样,您就不必担心自己处理请求。您可以自己检索各个零件,并将繁琐的工作留给容器。

关于java - 使用jsp作为服务器端脚本将文件上传到特定文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6756702/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com