Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7839971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:37:01+00:00 2026-06-02T15:37:01+00:00

i want to upload files(all format) to a server and store the file location

  • 0

i want to upload files(all format) to a server and store the file location in a database(MySQL)i tried storing the files(all format)in a database using long-blob format,but it is not accepting the file size of not more-then 1kb and it accepts only text file.
here is my html code

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <form enctype="multipart/form-data" action="upload.jsp" method="post">
        <br/><br/><br/>
        <center>
            <table border="0" bgcolor=#ccFDDEE>
                <tr>
                    <td colspan="2" align="center"><b>UPLOAD THE FILE</b></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"> </td>
                </tr>
                <tr>
                    <td><b>Choose the file To Upload:</b></td>
                    <td><INPUT NAME="file" TYPE="file">      </td>
                </tr>
                <tr>
                    <td colspan="2" align="center"> </td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="Send File"> </td>
                </tr>
            <table>
        </center> 
    </form>
</body>
</html>

here is my jsp code for storing files to a database(using long-blob).

<%@ page import="java.io.*,java.sql.*" %>
<%
String saveFile="";
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);
    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;
    saveFile="/home/adapco/Desktop"+saveFile;
    File ff = new File(saveFile);
    FileOutputStream fileOut = new FileOutputStream(ff);
    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>
<%
    Connection connection = null;
    String connectionURL = "jdbc:mysql://localhost:3306/ksa";
    ResultSet rs = null;
    PreparedStatement psmnt = null;
    FileInputStream fis;
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = DriverManager.getConnection(connectionURL, "root", "root");
        File f = new File(saveFile);
        psmnt = connection.prepareStatement("insert into file(file_data) values(?)");
        fis = new FileInputStream(f);
        psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length()));
        int s = psmnt.executeUpdate();
        if(s>0) {
            System.out.println("Uploaded successfully !");
        }
        else{
            System.out.println("unsucessfull to upload file.");
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
}
%>

now i have to store the files either to a server and storing its path to db or to store the files(all formats)in a database using long-blob type.iam using netbeans and glassfish server(red hat linux).give me some idea.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:37:02+00:00Added an answer on June 2, 2026 at 3:37 pm

    I strongly advice you use one of the existing modules people have built for multipart file upload handling. Commons Fileupload is on of the more popular, I suggest you look into it (usage example here).

    Your code is pretty messy and you are mixing working with byte arrays and Strings in a way that is sure to create errors. This:

    int boundaryLocation = file.indexOf(boundary, pos) - 4;
    int startPos = ((file.substring(0, pos)).getBytes()).length;
    int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
    ...
    fileOut.write(dataBytes, startPos, (endPos - startPos));
    

    is particularly hairy. Anyway, if you insist on doing it yourself, you should clean up your code, don’t mix working with Strings and byte arrays (remember, one character in a string is not necessarily just one byte). I don’t recommend it though, unless this is a learning experience for you. In which case I would spend some time to try and find some example code on the internet for multipart form handling.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to upload and store video files to my server using PHP. Could
I'm using the Uploadify plugin to upload files to my server, and I want
All I want to do is move a MySQL database from one server to
I want to upload files directly to IIS7 (in this case I am using
I want to upload data, images, txt files, pdf documents , etc. Im using
I have an application that let users to upload files to server. All files
I have a File.aspx form where I display all the files. When I upload
Hope you all will be fine. Actually i want to upload image files from
I want to upload a large file of maximum size 10MB to my MySQL
I am trying to upload a file to a ftp server using libcurl but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.