上传的PDF文件 [英] uploading of pdf file
问题描述
其中是给定代码中的错误???
filename-upload.html
<%@ page language =java%>
< HTml>
< HEAD>< TITLE>向用户显示文件上传表单< / TITLE>< / HEAD>
<%//用于上传我们使用的文件加密多部分/
形式数据的类型并输入文件类型来浏览和提交文件%>
< BODY> < FORM ENCTYPE =multipart / form-dataACTION =
uploadfile.htmlMETHOD = POST>
< br>< br>< br>
< center>< table border =2>
< tr>< center>< td colspan =2>< p align =
center>< B>上传文件的程序< / B><< ;中心],[< / TD>< / TR>
< tr>< td>< b>选择要上传的文件:< / b>
< / td>
< td>< INPUT NAME =F1TYPE =file>< / td>< / tr>
< tr>< td colspan =2>
< p align =right>< INPUT TYPE =submitVALUE =Send File>< / p>< / td>< / tr>
< table>
< / center>
< / FORM>
< / BODY>
< / HTML>
文件名 - uploadfile.html
<%@ page import =java.io. *%>
<%
//从JSP请求头获取内容类型信息
String contentType = request.getContentType();
//这里我们检查的内容类型不等于Null和
以及从mulitpart / form-data传递的数据大于或
等于0
如果((contentType!= null)&&(contentType.indexOf(multipart /
form-data)> = 0)){
DataInputStream in = new DataInputStream(request。
getInputStream());
//我们取内容类型数据的长度
int formDataLength = request.getContentLength();
字节dataBytes [] =新字节[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//此循环将上传的文件转换为字节码
while(totalBytesRead< formDataLength){
byteRead = in.read(dataBytes,totalBytesRead,
formDataLength);
totalBytesRead + = byteRead;
}
String file = new String(dataBytes);
//用于保存文件名$ b $ 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(=);
字符串边界= 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;
endPos =((file.substring( 0,boundaryLocation))
.getBytes())。length;
//创建一个具有相同名称的新文件并将
内容写入新文件
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes,startPos,(endPos-startPos));
fileOut.flush();
fileOut.close();
%>< Br>< table border =2>< tr>< td>< b>您已成功上传
文件的名称: < / b>
<%out.println(saveFile);%> < / TD>< / TR>< /表> <%
}
%>
这显然是一个 Roseindia 代码片段。首先,这是 最坏的 学习资源。不要使用它。它只教坏习惯。将该网站添加到您的黑名单。事实上,任何散布着广告横幅的教程网站,绝对过时的低质量代码片段,都是由业余爱好者明确维护的,主要关注广告收入,而不是严肃的教学。其他的这样的废话教程网站的例子是javabeat,tutorialspoint,journaldev,javatpoint等。这些网站有显着的共同点是,他们是在印度发源。
除了错误地使用 .html
文件扩展名而不是 .jsp
(尽管它们正确地显示了它们的示例与 .jsp
扩展名),代码片段有几个主要问题:
< font>
和< center>
标签,这些标签自1998年起被弃用。
Content-Length
请求头本身并不总是存在的。如果这个头文件不存在,代码就会中断。
String
数组使用服务器平台默认字符编码,而不是在多部分头中指定的字符编码。这可能malforma / corrupt结果字节。
DataInputStream
包装是不必要的,代码没有任何好处。 / li>
这是非常糟糕的。
从JSP上传文件的正确方法是将表单提交给 @MultipartConfig
注释的servlet类,然后使用 request.getPart()
来获取文件。你可以在这个答案找到一个片段:如何上传文件到服务器使用JSP / Servlet?
学习Java EE的正确方法详见这个答案: i want to upload a pdf file by using code given below.It give browsing facility but dont upload file. When i click sendfile button Its display uploadfile.html code page. How can i do that???
where is the error in the given code??? filename-upload.html filename--uploadfile.html
This is clearly a Roseindia code snippet. First of all, it is the worst learning resource ever. Don't use it. It only teaches bad practices. Add that site to your blacklist. In fact, any "tutorial" site which is littered with advertisement banners and hopelessly outdated low quality code snippets are clearly maintained by amateurs with primary focus on advertisement income instead of on serious teaching. Other examples of such crap "tutorial" sites are javabeat, tutorialspoint, journaldev, javatpoint, etc. Remarkable common thing which those sites have is that they are originated in India. Apart from the fact that you incorrectly used It's simply terrible. The right way to upload a file from JSP is to submit the form to a The right way to learn Java EE is elaborated in this answer: Java EE web development, where do I start and what skills do I need? 这篇关于上传的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!<%@ page language="java" %>
<HTml>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
<% // for uploading the file we used Encrypt type of multipart/
form-data and input of file type to browse and submit the file %>
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION=
"uploadfile.html" METHOD=POST>
<br><br><br>
<center><table border="2" >
<tr><center><td colspan="2"><p align=
"center"><B>PROGRAM FOR UPLOADING THE FILE</B><center></td></tr>
<tr><td><b>Choose the file To Upload:</b>
</td>
<td><INPUT NAME="F1" TYPE="file"></td></tr>
<tr><td colspan="2">
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p></td></tr>
<table>
</center>
</FORM>
</BODY>
</HTML>
<%@ page import="java.io.*" %>
<%
//to get the content type information from JSP Request Header
String contentType = request.getContentType();
//here we are checking the content type is not equal to Null and
as well as the passed data from mulitpart/form-data is greater than or
equal to 0
if ((contentType != null) && (contentType.indexOf("multipart/
form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.
getInputStream());
//we are taking the length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,
formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
//for saving the file name
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;
//extracting the index of file
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;
// creating a new file with the same name and writing the
content in new file
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully
upload the file by the name of:</b>
<% out.println(saveFile); %></td></tr></table> <%
}
%>
.html
file extension instead of .jsp
(even though they presented their examples correctly with .jsp
extensions), there are several major problems with the code snippet:
<font>
and <center>
tags which are deprecated since 1998.Content-Length
request header which is not always present per se. If this header is absent, the code breaks.String
based on the byte array using server platform default character encoding instead of the one specified in multi part header. This may malform/corrupt the result bytes.DataInputStream
wrapper is unnecessary, the code is not taking any benefit of it.
@MultipartConfig
annotated servlet class and then use request.getPart()
to get the file. You can find a snippet in this answer: How to upload files to server using JSP/Servlet?