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

  • Home
  • SEARCH
  • 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 6912123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:03:08+00:00 2026-05-27T09:03:08+00:00

I am using COS multipart to handle file upload on the servlet. When processing

  • 0

I am using COS multipart to handle file upload on the servlet.

When processing the parts i need to rename the file with an extra posted field (ParamPart), in this case ‘artikelcode’ needs to be prepended to the filename.

So instead of directly writing the FilePart to disk i need to save the inputstream in memory.

This is the code i have so far:

MultipartParser multipartParser = new MultipartParser(request, 100000000);
                String artikelcode = null;
                String filename = null;
                InputStream in = null;

                while ((part = multipartParser.readNextPart()) != null) {
                    if (part.isFile()) {
                        FilePart filePart = (FilePart) part;
                        filename = filePart.getFileName();
                        //long fileSize = filePart.writeTo(new File(fileSavePath));
                        if (filename != null) in = filePart.getInputStream();
                    }

                    if (part.isParam()) {
                        ParamPart paramPart = (ParamPart) part;
                        if (paramPart.getName().equals("artikelcode")) artikelcode = paramPart.getStringValue();
                    }
                }

                if (in != null)
                {
                    String fileSavePath =  "c:\\upload\\"+artikelcode+"_"+filename;
                    File file = new File(fileSavePath);
                    OutputStream out = new FileOutputStream(file);
                    IOUtils.copy(in, out);
                    out.close();
                }

When the file is saved on disk, it is empty!

Thanks for your help!!

  • 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-05-27T09:03:08+00:00Added an answer on May 27, 2026 at 9:03 am

    Calling readNextPart() invalidates any data that you got from the previous part.

    Here is a better approach: Always save the file with a temporary name and then rename it.

    This allows you to handle a lot of common errors graciously like: Disk full, errors while saving, etc. because you never overwrite existing files until you are 100% sure the new file is complete.

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

Sidebar

Related Questions

Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C#, I need a class called User that has a username, password, active
When plotting oscillations in R, e.g., using the package desolve, df1 <-function(t,y,mu)( list(c(y[2],mu*y[1]^3-y[1]+0.005*cos(t)))) library
I need to run multiple task in the background using backgroundwoker e.g dim mybacgroundwokers(10)
I have the following code doing Sin/Cos function using a pre-calculated memory table. in
I'm writing a program that is using the functions sin() and cos() from the
i am using the Persistent COS Naming service for binding objects in Java IDL
I'm trying to make a program to calculate the cos(x) function using taylor series
Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.

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.