jsp實現檔案上傳

來源:互聯網
上載者:User
upload.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>upload</title>
</head>
<body bgcolor="#ffffff">

<form name="form1" METHOD="POST" ACTION="receive.jsp" ENCTYPE="multipart/form-data">請選擇上傳檔案:
<INPUT TYPE="FILE" NAME="FILE1" SIZE="50" id="myfile">
<BR>
<INPUT TYPE="SUBMIT" VALUE="Upload">
</form>
</body>
</html>

receive.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<html>
<head>
<title>
receieve
</title>
</head>
<body bgcolor="#ffffff">
<br>
<%
String tempFile = new String("mytempfile");
       File temp = new File("D:"+File.separator, tempFile);
       FileOutputStream o = new FileOutputStream(temp);
       InputStream fileSource = null;
       fileSource = request.getInputStream();

       byte[] b = new byte[8192];
       int n;

       while((n = fileSource.read(b)) != -1) {
         o.write(b, 0, n);
         o.flush();
       }
       fileSource.close();
       o.close();


       RandomAccessFile rf = new RandomAccessFile(temp, "r");
       rf.readLine();
       String FilePath = rf.readLine();
       int position = FilePath.lastIndexOf('//');
       String filename = new String(FilePath.substring(position+1,FilePath.length()-1).getBytes("ISO-8859-1"),

"UTF-8");
       rf.seek(0);
       long forthEnterPosition = 0;
       int forth = 1;
       while((n = rf.readByte()) != -1 && (forth <= 4)) {
         if(n == 10) {
           forthEnterPosition = rf.getFilePointer();
           forth++;
         }
       }
File FileUploadDir = new File("D:"+File.separator, "upload");
       FileUploadDir.mkdir();

       File savefile = new File("D:"+File.separator+"upload", filename);

       RandomAccessFile rf2 = new RandomAccessFile(savefile, "rw");


       rf.seek(rf.length());
       long endPosition = rf.getFilePointer();

       int j = 1;
       while((endPosition>=0) && (j<=2)) {
         endPosition--;
         rf.seek(endPosition);
         if(rf.readByte() == 10)
         j++;
       }

       rf.seek(forthEnterPosition);
       long startPoint = rf.getFilePointer();
       while(startPoint + 8192L <= endPosition-1) {
         n = rf.read(b);
         startPoint = rf.getFilePointer();
         rf2.write(b, 0, n);
       }

       while(startPoint < endPosition-1) {
         rf2.write(rf.readByte());
         startPoint = rf.getFilePointer();
       }


       rf2.close();
       rf.close();
       temp.delete();
       out.println("file: " + filename + " succeed to upload!");
%>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.