• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Blob upload

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
I am trying to upload an image to a mysql database in a blob field. I have tried the code below in two different pcs. In the first pc all worked well. However in my home pc cpu load goes up to 100% and computer freezes, when the code is executed. Could you advise if something is wrong in the code , or it is a mysql issue.
Persistence entity has two fields,one byte[] and one of an object type

(I am using apache commons io and file upload jars)

@SuppressWarnings("serial")

public class Upload extends HttpServlet {
@Resource
private javax.transaction.UserTransaction utx;
@PersistenceContext( unitName = "RadiologistWebPU")
private EntityManager entityManager;
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Radiologicalimage radImage;
String myRequest = "RI0987";
byte[] myPic;
File saveFile=null;
String savePath = this.getServletConfig().getServletContext()
.getRealPath("");
savePath = savePath + "/" + myRequest + "/";
File f1 = new File(savePath);
System.out.println(savePath);
if (!f1.exists()) {
f1.mkdirs();
}
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("utf-8");
List fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
ex.toString();
}
Iterator<FileItem> it = fileList.iterator();
String name = "";
String extName = "";
while (it.hasNext()) {
FileItem item = it.next();
if (!item.isFormField()) {
name = item.getName();
long size = item.getSize();
String type = item.getContentType();
System.out.println(size + " " + type);
if (name == null || name.trim().equals("")) {
continue;
}

if (name.lastIndexOf(".") >= 0) {
extName = name.substring(name.lastIndexOf("."));
}
File file = null;
do {

file = new File(savePath + name);
} while (file.exists());
saveFile = new File(savePath + name );
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}

myPic = FileUtils.readFileToByteArray(saveFile);
radImage = new Radiologicalimage();
Radordersforschedule ro = new Radordersforschedule();
ro = entityManager.find(Radordersforschedule.class, "2");
radImage.setRadiologicalReportID(ro);
radImage.setRadiologicalImage(myPic);
persist(radImage);


}
}


response.getWriter().print(name + extName);
}

protected void persist(Object object) {
try {
utx.begin();
entityManager.persist(object);
utx.commit();
} catch (Exception e) {

System.out.println(e.toString());
}
}


}


 
rubbery bacon. crispy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic