我有一个严重的问题。问题是: java.awt.HeadlessException
现在的问题是,我在一个月前编写了相同的代码,并且在 Windows 7 和 NetBeans 7.1 中运行良好。不同之处在于他在 servlet 中编写了代码,但我是在 Java 文件中编写的,然后从 servlet 调用该方法。
BELIEVE ME IT'S 100% WORKS.
但现在我在 Windows 8 和 NetBeans 7.3 中只有这两个被改变。现在它不工作给出一个无头异常。我能做些什么???
现在请告诉我如何上传文件?我需要完整的目录路径,它将保存在数据库中。
注册.jsp:
<a href="UploadUserImage">
<input type="button" class="button round blue image-right ic-upload text-upper" value="Upload" name="upload"/>
</a>
<font style="font-family: Times New Roman; font-size: 16px; color: #2a2e36; font-style: italic;"><%= image %></font>
<input type="hidden" value="images/Member/<%= image %>" name="image"/>
UploadImage.java(Servlet):
String image="images/"+new UploadFile().Upload();
request.getRequestDispatcher("Register.jsp?image="+image).forward(request, response);
上传文件.java:
public class UploadFile
{
File file;
public String Upload()
{
try
{
final JFileChooser fc = new JFileChooser();
String[] extensions={"jpg", "png", "gif"};
FileNameExtensionFilter filter=new FileNameExtensionFilter("Images", extensions);
fc.setFileFilter(filter);
fc.setMultiSelectionEnabled(false);
//fc.setCurrentDirectory(new File("C:\\tmp"));
fc.setApproveButtonText("Upload");
int retVal = fc.showOpenDialog(new JPanel());
file=fc.getSelectedFile();
String src,dst;
src=file.getAbsolutePath();
dst="C:\\Users\\SHUVAM KAYAL\\Documents\\NetBeansProjects\\BookShopManagment\\BookShopManagment-war\\web\\images\\Member\\"+file.getName();
copy(new File(src), new File(dst));
}
catch (IOException ex)
{}
catch(NullPointerException e)
{}
return file.getName();
}
public void copy(File sourceLocation , File targetLocation) throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i=0; i<children.length; i++) {
copy(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
}