当前位置: 技术问答>java相关
怎么实现用JSP来upload文件?
来源: 互联网 发布时间:2015-07-14
本文导语: 有源代码最好,,,网站也行 | String PathStr="/admin/office/software/download/"; int count=0; String uploadfilename=null; String uploadFileSize=null; mySmartUpload.initialize(pageContext); mySmartUpload.setTotalMaxFileSize(20000...
有源代码最好,,,网站也行
|
String PathStr="/admin/office/software/download/";
int count=0;
String uploadfilename=null;
String uploadFileSize=null;
mySmartUpload.initialize(pageContext);
mySmartUpload.setTotalMaxFileSize(2000000000);
mySmartUpload.upload();
for (int i=0;i 0)
{
directory = file.substring(file.indexOf("name="Directory""));
//remove carriage return
directory = directory.substring(directory.indexOf("n")+1);
//remove carriage return
directory = directory.substring(directory.indexOf("n")+1);
//get Directory
directory = directory.substring(0,directory.indexOf("n")-1);
/*make sure user didn't select a directory higher in the directory tree*/
if (directory.indexOf("..") > 0)
{
out.println("Security Error: You can't upload " +"to a directory higher in the directory tree.");
return;
}
}
//get SuccessPage web variable from request
String successPage="";
if (file.indexOf("name="SuccessPage"") > 0)
{
successPage = file.substring(file.indexOf("name="SuccessPage""));
//remove carriage return
successPage = successPage.substring(successPage.indexOf("n")+1);
//remove carriage return
successPage = successPage.substring(successPage.indexOf("n")+1);
//get success page
successPage = successPage.substring(0,successPage.indexOf("n")-1);}
//get OverWrite flag web variable from request
String overWrite;
if (file.indexOf("name="OverWrite"") > 0)
{
overWrite = file.substring(file.indexOf("name="OverWrite""));
//remove carriage return
overWrite = overWrite.substring(
overWrite.indexOf("n")+1);
//remove carriage return
overWrite = overWrite.substring(overWrite.indexOf("n")+1);
overWrite = overWrite.substring(0,overWrite.indexOf("n")-1);
}
else
{
overWrite = "false";
}
//get OverWritePage web variable from request
String overWritePage="";
if (file.indexOf("name="OverWritePage"") > 0)
{
overWritePage = file.substring(file.indexOf("name="OverWritePage""));
//remove carriage return
overWritePage = overWritePage.substring(overWritePage.indexOf("n")+1);
//remove carriage return
overWritePage = overWritePage.substring(overWritePage.indexOf("n")+1);
//get overwrite page
overWritePage = overWritePage.substring(0,overWritePage.indexOf("n")-1);
}
//get filename of upload file
String saveFile = file.substring(file.indexOf("filename="")+10);
saveFile = saveFile.substring(0,saveFile.indexOf("n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\")+1,
saveFile.indexOf("""));
/*remove boundary markers and other multipart/form-data
tags from beginning of upload file section*/
int pos; //position in upload file
//find position of upload file section of request
pos = file.indexOf("filename="");
//find position of content-disposition line
pos = file.indexOf("n",pos)+1;
//find position of content-type line
pos = file.indexOf("n",pos)+1;
//find position of blank line
pos = file.indexOf("n",pos)+1;
/*find the location of the next boundary marker
(marking the end of the upload file data)*/
int boundaryLocation = file.indexOf(boundary,pos)-4;
//upload file lies between pos and boundaryLocation
file = file.substring(pos,boundaryLocation);
//build the full path of the upload file
String fileName = new String(rootPath + directory +
saveFile);
//create File object to check for existence of file
File checkFile = new File(fileName);
if (checkFile.exists())
{
/*file exists, if OverWrite flag is off, give
message and abort*/
if (!overWrite.toLowerCase().equals("true"))
{
if (overWritePage.equals(""))
{
/*OverWrite HTML page URL not received, respond
with generic message*/
out.println("Sorry, file already exists.");
}
else
{
//redirect client to OverWrite HTML page
response.sendRedirect(overWritePage);
}
return;
}
}
/*create File object to check for existence of
Directory*/
File fileDir = new File(rootPath + directory);
if (!fileDir.exists())
{
//Directory doesn't exist, create it
fileDir.mkdirs();
}
//instantiate file output stream
fileOut = new FileOutputStream(fileName);
//write the string to the file as a byte array
fileOut.write(file.getBytes(),0,file.length());
if (successPage.equals(""))
{
/*success HTML page URL not received, respond with
eneric success message*/
out.println(successMessage);
out.println("File written to: " + fileName);
}
else
{
//redirect client to success HTML page
response.sendRedirect(successPage);
}
}
else //request is not multipart/form-data
{
//send error message to client
out.println("Request not multipart/form-data.");
}
}
catch(Exception e)
{
try
{
//print error message to standard out
System.out.println("Error in doPost: " + e);
//send error message to client
out.println("An unexpected error has occurred.");
out.println("Error description: " + e);
}
catch (Exception f) {}
}
finally
{
try
{
fileOut.close(); //close file output stream
}
catch (Exception f) {}
try
{
in.close(); //close input stream from client
}
catch (Exception f) {}
try
{
out.close(); //close output stream to client
}
catch (Exception f) {}
}
}
}
int count=0;
String uploadfilename=null;
String uploadFileSize=null;
mySmartUpload.initialize(pageContext);
mySmartUpload.setTotalMaxFileSize(2000000000);
mySmartUpload.upload();
for (int i=0;i 0)
{
directory = file.substring(file.indexOf("name="Directory""));
//remove carriage return
directory = directory.substring(directory.indexOf("n")+1);
//remove carriage return
directory = directory.substring(directory.indexOf("n")+1);
//get Directory
directory = directory.substring(0,directory.indexOf("n")-1);
/*make sure user didn't select a directory higher in the directory tree*/
if (directory.indexOf("..") > 0)
{
out.println("Security Error: You can't upload " +"to a directory higher in the directory tree.");
return;
}
}
//get SuccessPage web variable from request
String successPage="";
if (file.indexOf("name="SuccessPage"") > 0)
{
successPage = file.substring(file.indexOf("name="SuccessPage""));
//remove carriage return
successPage = successPage.substring(successPage.indexOf("n")+1);
//remove carriage return
successPage = successPage.substring(successPage.indexOf("n")+1);
//get success page
successPage = successPage.substring(0,successPage.indexOf("n")-1);}
//get OverWrite flag web variable from request
String overWrite;
if (file.indexOf("name="OverWrite"") > 0)
{
overWrite = file.substring(file.indexOf("name="OverWrite""));
//remove carriage return
overWrite = overWrite.substring(
overWrite.indexOf("n")+1);
//remove carriage return
overWrite = overWrite.substring(overWrite.indexOf("n")+1);
overWrite = overWrite.substring(0,overWrite.indexOf("n")-1);
}
else
{
overWrite = "false";
}
//get OverWritePage web variable from request
String overWritePage="";
if (file.indexOf("name="OverWritePage"") > 0)
{
overWritePage = file.substring(file.indexOf("name="OverWritePage""));
//remove carriage return
overWritePage = overWritePage.substring(overWritePage.indexOf("n")+1);
//remove carriage return
overWritePage = overWritePage.substring(overWritePage.indexOf("n")+1);
//get overwrite page
overWritePage = overWritePage.substring(0,overWritePage.indexOf("n")-1);
}
//get filename of upload file
String saveFile = file.substring(file.indexOf("filename="")+10);
saveFile = saveFile.substring(0,saveFile.indexOf("n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\")+1,
saveFile.indexOf("""));
/*remove boundary markers and other multipart/form-data
tags from beginning of upload file section*/
int pos; //position in upload file
//find position of upload file section of request
pos = file.indexOf("filename="");
//find position of content-disposition line
pos = file.indexOf("n",pos)+1;
//find position of content-type line
pos = file.indexOf("n",pos)+1;
//find position of blank line
pos = file.indexOf("n",pos)+1;
/*find the location of the next boundary marker
(marking the end of the upload file data)*/
int boundaryLocation = file.indexOf(boundary,pos)-4;
//upload file lies between pos and boundaryLocation
file = file.substring(pos,boundaryLocation);
//build the full path of the upload file
String fileName = new String(rootPath + directory +
saveFile);
//create File object to check for existence of file
File checkFile = new File(fileName);
if (checkFile.exists())
{
/*file exists, if OverWrite flag is off, give
message and abort*/
if (!overWrite.toLowerCase().equals("true"))
{
if (overWritePage.equals(""))
{
/*OverWrite HTML page URL not received, respond
with generic message*/
out.println("Sorry, file already exists.");
}
else
{
//redirect client to OverWrite HTML page
response.sendRedirect(overWritePage);
}
return;
}
}
/*create File object to check for existence of
Directory*/
File fileDir = new File(rootPath + directory);
if (!fileDir.exists())
{
//Directory doesn't exist, create it
fileDir.mkdirs();
}
//instantiate file output stream
fileOut = new FileOutputStream(fileName);
//write the string to the file as a byte array
fileOut.write(file.getBytes(),0,file.length());
if (successPage.equals(""))
{
/*success HTML page URL not received, respond with
eneric success message*/
out.println(successMessage);
out.println("File written to: " + fileName);
}
else
{
//redirect client to success HTML page
response.sendRedirect(successPage);
}
}
else //request is not multipart/form-data
{
//send error message to client
out.println("Request not multipart/form-data.");
}
}
catch(Exception e)
{
try
{
//print error message to standard out
System.out.println("Error in doPost: " + e);
//send error message to client
out.println("An unexpected error has occurred.");
out.println("Error description: " + e);
}
catch (Exception f) {}
}
finally
{
try
{
fileOut.close(); //close file output stream
}
catch (Exception f) {}
try
{
in.close(); //close input stream from client
}
catch (Exception f) {}
try
{
out.close(); //close output stream to client
}
catch (Exception f) {}
}
}
}
|
jspsmartUpload
me 有,给ME 发信
chenshenglei@yahoo.com.cn
me 有,给ME 发信
chenshenglei@yahoo.com.cn
|
我按网上见到的哪个servlet例子来写,但不能上传2进制文件/文本类的可以。
主要思路是用request的getInputStream();然后还原。。。
主要思路是用request的getInputStream();然后还原。。。
|
要自己写啊,赞同!!当时,我也这么想来着。
我用request的getInputStream()方法得到上传上来的内容,
包括非文件表单和文件表单,主要是把各个表单处理一下,
感觉文本还可以,其他的有点困难,主要是看不到格式,
我用request的getInputStream()方法得到上传上来的内容,
包括非文件表单和文件表单,主要是把各个表单处理一下,
感觉文本还可以,其他的有点困难,主要是看不到格式,