File upload by JSP (轉)
In forum, a lot of people are looking for another way to file by ,other than SmartUpload,which is a free bean from jspsmart.com.
Although its functionality is powerful, at least called "enough", the code is invisible to us,which is a little pity of this bean.
A few days ago,I got an article talking about the upload things on on.com.
and the author provd a piece of code to implement upload feature.
Click to enjoy it!
However, there is also a flaw in his code, that's it only supports text plain file but binary format.In fact, most of files to upload are in binary format,aren't they?
But don't worry, I have made some modification on the original sourcecode to enable it to support all file formats.
Now I paste the whole source code below,
hopefully it can be helpful to those who are interested in this problem.
BTW: My E is , welcome to send mail and discuss with me on any Java technical issues.
Code:
import javax..http.HttpServletRequest;
import javax.servlet.ServletInputStream;
import java.util.Dictionary;
import java.util.Hashtable;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class FileUploaean{
private String savePath, filepath, filename, contentType;
private Dictionary fields;
//Begin Rayman's modification
private static final int BUF_SIZE = 256;
//End Rayman's modification
public String getFilename() {
return filename;
}
public String getFilepath() {
return filepath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getContentType() {
return contentType;
}
public String getFieldValue(String fieldName) {
if (fields == null ¦¦ fieldName == null)
{
//System.out.println(fieldName);
//System.out.println("if l");
return null;
}
//System.out.println(fieldName);
//System.out.println(fields.get("company"));
return (String)fields.get(fieldName);
}
private void setFilename(String s) {
if (s==null)
return;
int p= s.indexOf("filename="");
if (pos != -1) {
filepath = s.substring(pos+10, s.length()-1);
// browsers include the full path on the client
// But / and Mac browsers only send the filename
// test if this is from a Windows browser
pos = filepath.lastIndexOf("");
if (pos != -1)
filename = filepath.substring(pos + 1);
else
filename = filepath;
}
}
private void setContentType(String s){
if (s==null)
return;
int pos = s.indexOf(": ");
if (pos != -1)
contentType = s.substring(pos+2, s.length());
}
public void doUpload(HttpServletRequest request) throws IOException{
ServletInputStream in = request.getInputStream();
byte[] line = new byte[BUF_SIZE];
int i = in.readLine(line, 0, 128);
if (i < 3)
return;
int boundaryLength = i - 2;
String boundary = new String(line, 0, boundaryLength); //-2 discards the newline character
fields = new Hashtable();
while (i != -1) {
String newLine = new String(line, 0, i);
if (newLine.startsWith("Content-Disposition: form-data; name="")) {
if (newLine.indexOf("filename="") != -1) {
setFilename(new String(line, 0, i-2));
if (filename==null)
return;
//this is the file content
i = in.readLine(line, 0, 128);
setContentType(new String(line, 0, i-2));
i = in.readLine(line, 0, 128);
//Begin Rayman's modification
FileOutputStream pw = new FileOutputStream((savePath==null? "" : savePath) + filename);
boolean done = false;
int k = 0;
byte c1 = 0x0d;
byte c2 = 0x0a;
byte [] boundarybytes = boundary.getBytes();
byte [] magickey = new byte[boundarybytes.length + 2];
magickey[0] = c1;
magickey[1] = c2;
for(int t = 0;t
magickey[2+t] = boundarybytes[t];
}
byte [] buf = new byte[magickey.length];
int count = 0;
byte c = -1;
boolean pause = false;
while (true) {
// the problem is the last line of the file content
// contains the new line character.
// So, we need to check if the current line is
// the last line
if(!pause)
{
byte [] one = new byte[1];
if(in.read(one)==-1) break;
c = one[0];
count ++;
}
else
{
pause = false;
}
if(c==magickey[k])
{
System.out.print((int)c + " " + (char)c + " ");
if(k == magickey.length-1)
{
//It's last line.
System.out.println("Last line!" + " count==" + count);
break;
}
buf[k] = (byte)c;
k++;
}
else
{
if(k>0)
{
pw.write(buf,0,k);
}
if(c==magickey[0])
{
pause = true;
}
else
{
pw.write(c);
}
k=0;
}
}
pw.close();
//End Rayman's modification
}
else {
//this is a field
// get the field name
int pos = newLine.indexOf("name="");
String fieldName = newLine.substring(pos+6, newLine.length()-3);
//System.out.println("fieldName:" + fieldName);
// blank line
i = in.readLine(line, 0, 128);
i = in.readLine(line, 0, 128);
newLine = new String(line, 0, i);
StringBuffer fieldValue = new StringBuffer(128);
while (i != -1 && !newLine.startsWith(boundary)) {
// The last line of the field
// contains the new line character.
// So, we need to check if the current line is
// the last line.
i = in.readLine(line, 0, 128);
if ((i==boundaryLength+2 ¦¦ i==boundaryLength+4) // + 4 is eof
&& (new String(line, 0, i).startsWith(boundary)))
fieldValue.append(newLine.substring(0, newLine.length()-2));
else
fieldValue.append(newLine);
newLine = new String(line, 0, i);
}
//System.out.println("fieldValue:" + fieldValue.toString());
fields.put(fieldName, fieldValue.toString());
}
}
i = in.readLine(line, 0, 128);
} // end while
}
public FileUploadBean()
{
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-991215/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Fckeditor PHP/ASP File Upload VulPHP
- html 5 drag and drop upload fileHTML
- Upload excel file 到 INTERNAL TABLE 的方法Excel
- FIneCMS /dayrui/libraries/Chart/ofc_upload_image.php Arbitrary File Upload VulUIPHP
- Salesforce LWC學習(二十七) File UploadSalesforce
- angular-file-upload 在IE下使用的坑Angular
- 上傳報錯:Upload Failed: Your upload has failed a virus scan. Please choose another file.AI
- 實用且常用的FN:Upload Excel File Into Internal TableExcel
- JSP檔案管理後門工具jsp-file-browserJS
- el-upload拍照上傳多個檔案報錯 ERR_UPLOAD_FILE_CHANGED問題
- 採用Docker整合jquery-file-upload元件到WEB應用DockerjQuery元件Web
- jquery ajax file upload NET MVC 無重新整理檔案上傳jQueryMVC
- php 3個多檔案上傳(uploadify、file upload、 webuploader)PHPWeb
- JSP和windows(轉)JSWindows
- qibocms /hr/listperson.php File Arbitrarily Include Vul Via Variable Uninitialization && Front Page Upload WEBSHELLPHPWebshell
- jquery uploadjQuery
- Upload Files
- JSP開發入門(三)----JSP與JavaBean (轉)JSJavaBean
- JSP EL表示式(轉)JS
- JSP自學筆記 (轉)JS筆記
- JSP語法格式 (轉)JS
- JSP Session處理 (轉)JSSession
- jsp設計模式(轉)JS設計模式
- (轉)innodb_doublewrite_file
- Servlet.service() for Servlet jsp threw exception javax.servlet.ServletException:File "/pageFooServletJSExceptionJava
- JSP 語法詳解(轉)JS
- The Ultimate (DLL) Header File (轉)Header
- JSP開發入門(四)----JSP的內部物件 (轉)JS物件
- wordpress /wp-content/plugins/wp-symposium/server/php/UploadHandler.php File Arbitrary Upload VulPluginServerPHP
- CVE2017-12615漏洞復現( tomcat JSP Upload Bypass /Remote Code Execution)TomcatJSREM
- JSP開發入門(五)----JSP其他相關資源 (轉)JS
- Spring Boot 新增JSP支援【轉】Spring BootJS
- jsp語法轉換為thyemleafJS
- 處理JSP中的中文 (轉)JS
- ASP+和JSP大比拚 (轉)JS
- 求救!!!!!!jsp執行錯誤;(轉)JS
- Java Servlet和JSP教程(6)(轉)JavaServletJS
- Java Servlet和JSP教程(7)(轉)JavaServletJS